PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Aug 21, 2004 5:13 am
Code: Select all
<?php
##### function for checking if server is online
function server_check ($server) {
if ($fp = @fsockopen ($server)) { // succesful
$result = 'Online';
@fclose ($fp);
} else { // failed
$result = 'Offline';
}
return $result;
} ## /function for checking if server is online
####### parse template
$pattern = "#\{%server=(.*?)%\}#is";
$output['body'] = preg_replace ($pattern, server_check ('\\1'), $output['body']);
?>
how can i replace the pattern with the result of the server_check()-function?
the function should be called with the result of the preg_replace.
also, how can i make this "ping"-script work?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Aug 21, 2004 10:16 am
Code: Select all
preg_replace('#blah#e','''blahblah'' . highlight_string(''\\1'')',$foo);
although you might like [php_man]preg_replace_callback[/php_man] more..
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Aug 21, 2004 11:12 am
so, something like this:
Code: Select all
<?php
$output['body'] = preg_replace_callback ($pattern, server_check ('$matches'), $output['body']);
?>
im afraid it doesn't work, now the string doesn't get replaced at all
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Aug 21, 2004 11:19 am
Code: Select all
$output['body'] = preg_replace_callback( $pattern, 'server_check', $output['body']);
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Aug 21, 2004 11:20 am
great, now I just need to fix the "pinger"
any advices regarding it?
it just outputs Offline even if my computer is online
Code: Select all
<?php
##### function for checking if server is online
function server_check ($server) {
if ($fp = @fsockopen ($server[1])) { // succesful
$result = 'Online';
@fclose ($fp);
} else { // failed
$result = 'Offline';
}
return $result;
} ## /function for checking if server is online
####### parse output
$pattern = "#\{%server=(.*?)%\}#is";
$output['body'] = preg_replace_callback ($pattern, 'server_check', $output['body']);
?>
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Aug 21, 2004 3:22 pm
looks rather not-cross-os-available :p
are there any other ways of checking if an server/ip is onlinem while sending and recieving as little data as possible?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Aug 21, 2004 3:26 pm
if you want to see if http is running.. ask curl to just return the header.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Aug 21, 2004 3:49 pm
i don't really get CURL, how does it work? seems rather complicated ><
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Aug 21, 2004 3:55 pm
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sat Aug 21, 2004 4:21 pm
looks interesting, but what does all those setopt('') things do, and how should i set it up too make it only look if the server's up?
sorry for my n00bish questions, put i can't figure out how it works ><
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Sat Aug 21, 2004 5:05 pm
The setopt calls set options
[php_man]curl_setopt[/php_man]()...
curl_exec will return with a failure of some sort if the server isn't up.
vigge89
Forum Regular
Posts: 875 Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden
Post
by vigge89 » Sun Aug 22, 2004 3:16 am
i solved it by using:
Code: Select all
<?php
##### function for checking if server is online
function server_check ($server) {
$fp = @fsockopen ($server[1], 80);
if ($fp) { // succesful
$result = 'Online';
@fclose ($fp);
} else { // failed
$result = 'Offline';
}
return $result;
} ## /function for checking if server is online
####### parse output
$pattern = "#\{%server=(.*?)%\}#is";
$output['body'] = preg_replace_callback ($pattern, 'server_check', $output['body']);
?>
Thanks for the replies feyd