Forum Moderators: coopster

Message Too Old, No Replies

basic server status

         

lindajames

4:40 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



Can anyone tell me how i can create a PHP script that checks the current status of any server by IP address and the port number to check? for example 192.168.0.1:21 if thats responding it shoudl display 1 image and if not then it should display the second image.

any suggestions would be appreciated.

Cheers
Linda

dmorison

5:59 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



fsockopen() should do the trick

$ip = "192.168.0.1";
$port = "21";

$fp = fsockopen($ip,$port);

if ($fp)
{
echo("<img src='image1.gif'>");
fclose($fp);
}
else
{
echo("<img src='image2.gif'>");
}

[edited by: dmorison at 8:24 pm (utc) on July 13, 2003]

lindajames

6:36 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



i actually tried that but my host seems to not execute that properly, maybe they dont have fsockopen(), i forgot to mention its PHP4 on IIS, will that help?

Cheers
Linda

hakre

7:22 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi lindajames,

my host seems to not execute that properly, maybe they dont have fsockopen()

if you're using win9x, fsockopen does not work well. this bug is known and describben in the php documentation.

in general it's good to know what does not work in detail.

if you're not able to connect to another server then maybe it's blocked by the firewall of your hosting company / server or the counterpart. can you validate this with other (non-php) tools?

or does the function not exist at all and you'll a get php error message? modify the error-settings to get a detailed error/warning message, here is how:


error_reporting(E_ALL);

maybe then it's easier to find out what is going wrong.

- hakre.

lindajames

7:34 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



i seem to get this message:

Fatal error: Call to undefined function: ()

Any idea?

Cheers
Linda

hakre

8:32 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




i seem to get this message:
Fatal error: Call to undefined function: ()

the name of the function is not displayed? then maybe this function has been disabled with your php version. is it your server? can you verify if safemode is on or off or which functions are disabled? maybe with phpinfo() or ini_get() [de2.php.net].

lindajames

8:36 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



no its not my server im just hosting my site. i've done the phpinfo() thing, what am i actually looking for?

hakre

8:44 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1.) is safemode on or off?
2.) are specific functions disabled and which?

if phpinfo is not the good, try the second hint. the configuration directives are:

safe_mode
and
disable_functions

lindajames

8:46 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



for safe_mode i've got "off" and for disable_functions i've got "no value"

hakre

8:47 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



then i assume it's a syntax error. can you post the line(s) of interest?

lindajames

8:49 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



<?php

$ip = "213.31.111.11";
$port = "21";
$fp = $fsockopen($ip,$port);

if ($fp)
{
echo("<img src='image1.gif'>");
fclose($fp);
}
else
{
echo("<img src='image2.gif'>");
}

?>

[edited by: jatar_k at 4:58 pm (utc) on July 14, 2003]
[edit reason] generalized ip [/edit]

hakre

8:52 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$fp = $fsockopen($ip,$port);

remove the $ in front of

fsockopen
, otherwise the function named by the value of the variable $fsockopen will be called which is empty and produces the error message.

for more info read about variable functions [de.php.net] in php.

this should help! ;)

lindajames

8:55 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



thanx alot it seems to be working now, one question though. is when is image1.gif shown and when is image2.gif shown?

hakre

9:01 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



to make it a long description ;) :

image1.gif is shown if the fsockopen function was able to open a socket connection to the port on the host specified by the ip-address. for example, if port 80 is opened, often there is a webserver running (www-service).

the other image is shown if the connection fails.

you can read more about fsockopen at http://www.php.net/fsockopen [php.net].

in short ;) :
status up.

- hakre.

lindajames

9:08 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



thanx hakre,

image1.gif seems to work but if i change the port to something that is most likely not open i.e. 81 then the script doesnt display image2.gif instead the script takes forever to load and eventually times-out

any suggestions?

Cheers
Linda

lindajames

9:57 pm on Jul 13, 2003 (gmt 0)

10+ Year Member



it seems that my host has a time-out limit and therefore the script doesnt get the time to actually respond to say the ip:port is down.

any suggestions would be appreciated.

Cheers
Linda

hakre

10:10 pm on Jul 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi linda,

you can specify how long fsockopen will try to connect until timeout (of the socket opening, not the script ;) ):


int fsockopen ( string target, int port [, int errno [, string errstr [, float timeout]]])

in your case:


$fp = fsockopen($ip,$port,$errno,$errstr,30);

tries to connect for 30 seconds.