Hello fellow developers of Stack Overflow! I recently got back into web development (although I wasn't too good at it before) with PHP being my weapon of choice. PHP seems to have changed since I was out of it, and combine that with the fact that I have never used PHP sockets, before lead to a disastrous first attempt to create an IRC bot (I am on an IRC channel where bot developing is big, and I want to integrate it into my website. Writing it in php also seems like a fun challenge). It created an infinite loop that made my browser go slow, and I wasn't able to copy any errors or warnings. Would the good folks of so mind looking over this script (based on this bot):
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Lucky Cloud</title>
</head>
<body>
<?php
error_reporting(E_ERROR);
$bot = array(
"Host" => "irc.quakenet.org",
"Channels" => ["#cplusplus", "#BotDevGroundZero"],
"Nick" => "LuckyCloud",
"Ident" => "LuckyCloud",
"Real" => "LuckyCloud",
"Port" => 6667
);
$buffer = "";
?>
<p>
Server: <?php echo $bot["Host"]; ?><br />
Channel(s): <?php foreach($bot["Channels"] as $channel) echo $channel.($channel != end($bot["Channels"]) ? ", " : ""); ?><br />
Port: <? echo $bot["Port"]; ?><br />
___________________________________________________________________________________________________________________<br />
</p>
<?php
global $socket;
$socket = fsockopen($bot["host"], $bot["Port"]);
function sendData($cmd, $msg = null) {
if($msg == null) {
fputs($socket, $cmd."\r\n");
echo "<strong>".$cmd."</strong><br />";
}
else {
fputs($socket, $cmd." ".$msg."\r\n");
echo "<strong>".$cmd." ".$msg."</strong><br />";
}
}
sendData("NICK", $bot["Nick"]);
sendData("USER", $bot["Ident"]." ".$bot["Host"]." ".$bot["Real"]);
$buffer = "";
while(true) {
foreach($bot["Channels"] as $channel) {
sendData("JOIN", $channel);
}
$buffer += fgets($socket, 1024);
$temp = explode("\n", $buffer);
$buffer = end($temp);
foreach($temp as $line) {
echo $line;
$line = rtrim($line);
$line = explode($line);
if($line[0] == "PING") {
sendData("PONG", $line[1]);
}
}
}
?>
</body>
</html>
sorry for any formatting issues. the cpanel editor was acting weird
PHP is not the best thing for this job. It's really not good at keeping long running connections. This script might be used for short time joins, where you simply drop a message and leave. Something like "event based notification".
Several additions to your script:
while (!feof($socket)) {
You wont be able to implement it this way, what's causing the issue is simply that once the
while(true)
loop starts it wont stop.You will need to separate the request from the processing loop. So put the loop as a background process (daemon) and then push your PINGS and PONGS ect to it via another interface like for example a
threads
database, then on each iteration of the loop, query the database for your PINGS and PONGS.