I am using the PHP script below to log an authenticated user into a chat channel. The problem is that the page needs to continuously load to keep the user logged in. I make this happen by using a while
loop that stops running when $timer
reaches 10 million (enough time in microseconds for successful login). This limit is to prevent an infinite loop. If I leave out this while
loop, the user will never be logged in.
How do I allow the page to stop loading after a normal time and keep the user logged in? I just don’t know where to go from here.
$chatServer = [];
ChatLogin('asdfghost_', 'test_channel');
function ChatLogin($user, $target_ch) {
set_time_limit(0);
function IRC($name, $channel) {
$config = array(
'server' => 'irc.twitch.tv',
'port' => 6667,
'channel' => '#'.$channel,
'name' => $name,
'nick' => $name,
'pass' => 'oauth:'.$GLOBALS['theToken']
);
$GLOBALS['chatServer']['connect'] = fsockopen($config['server'], $config['port']);
if ($GLOBALS['chatServer']['connect']) {
SendData("PASS " . $config['pass'] . "\n\r");
SendData("NICK " . $config['nick'] . "\n\r");
SendData("USER " . $config['nick'] . "\n\r");
SendData("JOIN " . $config['channel'] . "\n\r");
$timer = 0; // load time
while (!feof($GLOBALS['chatServer']['connect']) && $timer <= 10000000) {
++$timer;
}
echo "Now connected to " . $channel . "’s channel!";
} else {
echo "Could not connect to channel. Try again.";
}
}
function SendData($cmd) {
fwrite($GLOBALS['chatServer']['connect'], $cmd, strlen($cmd));
}
IRC($user, $target_ch);
}