频道内 IRC 在线计数
我发现的脚本有问题。有时它有效,有时它不起作用......任何人都可以看到它有什么问题吗?或者有更好的吗?
非常感谢
<?php
/* CONFIGURATIONS! */
$irc_server = 'xxxxxxxxxx'; // Server
$irc_port = '6667'; // Port
//$irc_nick = ''; // Nick
$irc_channel = '#xxxxxxxx'; // Channel
/* END CONFIGURATIONS! */
$socket = fsockopen($irc_server,$irc_port); // Connect to the server.
fputs($socket,"USER SOCKS SOCKS SOCKS :SOCKS\r\n"); // Send the username.
fputs($socket,"NICK $irc_nick \r\n"); // Set our nick.
fputs($socket,"LIST $irc_channel \r\n"); // List the provided channel.
// $handle = fopen('log.txt',a); // Log if you want.
// Set initial value.
$users = 0;
// Receive the incoming data.
while(1) {
$sock_data = fgets($socket,1024); // Get each line of data from the response.
// fwrite($handle,"$sock_data\r\n"); // Write the log.
// Get the information section of the desired responses.
$sep = explode(':',$sock_data); // Separate by colons.
$info = $sep[1]; // After the first colon is the important information.
$message = $sep[2];
// Break down the response and get the id and who sent it.
$info_sep = explode(' ',$info); // Separate by spaces.
$full_who = $info_sep[0]; // The person who sent it is the first item in the list.
$id = $info_sep[1]; // The id is the second item in the list.
$who_sep = explode('!',$full_who); // Separate by exclamation points.
$who = $who_sep[0]; // The nick of the person is the part before the exclamation point.
// I saw some scripts checking for this, so...
if ($who == 'PING') {
fputs($socket,"PONG $message");
}
// PRIVMSG indicates someone is sending you a message.
// We just need this to reply to the VERSION and PING requests.
if ($id == 'PRIVMSG') {
if (substr($message, 0, 8) == 'VERSION') { // Reply to the version response.
fputs($socket,"NOTICE $who :".chr(1)."VERSION getUsers v0.1b".chr(1)."\r\n");
} elseif (strstr($message, 'PING') !== false) { // Ping them back if needed.
fputs($socket,"NOTICE $who :$message");
}
}
// 322 is the list response.
// This should get the number of users on the provided channel.
if ($id == '322') {
$users = $info_sep[4]; // The number of users.
// fclose($handle); // Close the log.
fclose($socket); // Close the connection.
break; // End the loop.
}
// 323 is the end list response.
// This is in case there is no 322 response (the channel doesn't exist, maybe?)
if ($id == '323') {
// fclose($handle); // Close the log.
fclose($socket); // Close the connection.
break; // End the loop.
}
// 263 is the failed response.
// Wait 2 seconds and retry.
if ($id == '263') {
sleep(2); // Pause for 2 seconds.
fputs($socket,"LIST $irc_channel \r\n"); // List the provided channel.
}
}
// Display the results on the page.
if ($users == '1') {
echo "1";
} else {
echo "$users";
}
?>
Am having problem's with a script I found. Sometimes it works and sometimes it don't... Can anyone see any problems with it? or have a better one?
Many Thanks
<?php
/* CONFIGURATIONS! */
$irc_server = 'xxxxxxxxxx'; // Server
$irc_port = '6667'; // Port
//$irc_nick = ''; // Nick
$irc_channel = '#xxxxxxxx'; // Channel
/* END CONFIGURATIONS! */
$socket = fsockopen($irc_server,$irc_port); // Connect to the server.
fputs($socket,"USER SOCKS SOCKS SOCKS :SOCKS\r\n"); // Send the username.
fputs($socket,"NICK $irc_nick \r\n"); // Set our nick.
fputs($socket,"LIST $irc_channel \r\n"); // List the provided channel.
// $handle = fopen('log.txt',a); // Log if you want.
// Set initial value.
$users = 0;
// Receive the incoming data.
while(1) {
$sock_data = fgets($socket,1024); // Get each line of data from the response.
// fwrite($handle,"$sock_data\r\n"); // Write the log.
// Get the information section of the desired responses.
$sep = explode(':',$sock_data); // Separate by colons.
$info = $sep[1]; // After the first colon is the important information.
$message = $sep[2];
// Break down the response and get the id and who sent it.
$info_sep = explode(' ',$info); // Separate by spaces.
$full_who = $info_sep[0]; // The person who sent it is the first item in the list.
$id = $info_sep[1]; // The id is the second item in the list.
$who_sep = explode('!',$full_who); // Separate by exclamation points.
$who = $who_sep[0]; // The nick of the person is the part before the exclamation point.
// I saw some scripts checking for this, so...
if ($who == 'PING') {
fputs($socket,"PONG $message");
}
// PRIVMSG indicates someone is sending you a message.
// We just need this to reply to the VERSION and PING requests.
if ($id == 'PRIVMSG') {
if (substr($message, 0, 8) == 'VERSION') { // Reply to the version response.
fputs($socket,"NOTICE $who :".chr(1)."VERSION getUsers v0.1b".chr(1)."\r\n");
} elseif (strstr($message, 'PING') !== false) { // Ping them back if needed.
fputs($socket,"NOTICE $who :$message");
}
}
// 322 is the list response.
// This should get the number of users on the provided channel.
if ($id == '322') {
$users = $info_sep[4]; // The number of users.
// fclose($handle); // Close the log.
fclose($socket); // Close the connection.
break; // End the loop.
}
// 323 is the end list response.
// This is in case there is no 322 response (the channel doesn't exist, maybe?)
if ($id == '323') {
// fclose($handle); // Close the log.
fclose($socket); // Close the connection.
break; // End the loop.
}
// 263 is the failed response.
// Wait 2 seconds and retry.
if ($id == '263') {
sleep(2); // Pause for 2 seconds.
fputs($socket,"LIST $irc_channel \r\n"); // List the provided channel.
}
}
// Display the results on the page.
if ($users == '1') {
echo "1";
} else {
echo "$users";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能会更幸运地使用经过测试且稳定的 PHP IRC 库,例如 PEAR 的 Net_SmartIRC - http://pear.php。净/包/Net_SmartIRC
You might have more luck with a tested and stable PHP IRC library like PEAR's Net_SmartIRC - http://pear.php.net/package/Net_SmartIRC