Click to See Complete Forum and Search --> : Check if ip is online or offline?


Pixel-Artist
02-21-2006, 02:43 PM
How do I do this. I've tried fread and stuff and never get it to work right. It is ip and port.

xx.xxx.xx.xxx:xxxx

please help

NogDog
02-21-2006, 03:24 PM
I'd probably try fsockopen() (http://www.php.net/fsockopen).

ShrineDesigns
02-22-2006, 03:55 AM
this checks if a website existsfunction lookup_url($url)
{
$path = '/';
$port = getservbyname('www', 'tcp');

$t = parse_url($url);

if(!array_key_exists('host', $t))
{
return array(404, 'Not Found');
}
$host = $t['host'];

if(array_key_exists('port', $t))
{
$port = $t['port'];
}
if(array_key_exists('path', $t))
{
$path = $t['path'];
}
if(array_key_exists('query', $t))
{
$path .= '?' . $t['query'];
}
$sp = @socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
@socket_set_timeout($sp, 10);

if(!$sp || !@socket_connect($sp, gethostbyname($host), $port))
{
return array(503, 'Service Unavailable');
}
@socket_write($sp, "HEAD $path HTTP/1.0\r\nHost: $host\r\nConnection: Close\r\n\r\n");
$r = @socket_read($sp, 1024);
@socket_close($sp);
$r = explode("\r\n", $r, 2);
$r = explode(' ', $r[0]);
return array_slice($r, 1, 2);
}if you need to check an ip address you need to do a ping test, i can't remember if php had a ping function, but you can use exec() with the ping program on the system unix and windows (i think only NT, 2k, and XP) has one

Pixel-Artist
02-22-2006, 02:17 PM
I did this...


<?php
$ip1 = "XX.XXX.XX.XXX:XXXX";
$up1 = @fsockopen("$ip", 80, $errno, $errstr, 30);
if($up1){
echo '<b><font color="green">Online</font></b>';
}else{
echo '<b><font color="red">Offline</font></b>';
}
?>

felgall
02-22-2006, 02:55 PM
You are better off checking by domain name rather than IP address since many different sites can share one IP address and one site may be offline while others at the same IP are online.

If you are trying to check IP addresses for users then it wont work at all since in most cases once a user goes offline their IP is reallocated to someone else.

Pixel-Artist
02-22-2006, 03:05 PM
Its for a gaming server.