note 60292 added to function.shell-exec

From: Date: Mon, 02 Jan 2006 17:06:50 +0000
Subject: note 60292 added to function.shell-exec
Groups: php.notes 
Request: Send a blank email to php-notes+get-101112@lists.php.net to get a copy of this message
As a webmaster i often deal with other servers with respect to the one that hosts my own site. I actually had to face the following problem: some web pages depend directly from other webservers for example including a IMG SRC tag pointing to a remote image; but what if the remote server is temporary down? The download of the page remains in an incomplete state. It'd be better an error message for sure ! But how can i know whether the remote server is up or down? PING IT is the answer..but how can i use a shell system function that outputs only text into some PHP scripting?..here the solution: <?php /* the host whose service status has to be checked out */ $host_to_ping = "www.somehost.net" ; /* I ping twice the host (a higher number of ping would only result the execute script time to be MUCH high). The result of the output ping command is stored into a variable. This can be done beacuse of the properties and features of the shell_exec() function, see above to know more. */ $outputShell = shell_exec("ping -c2 -w2 $host_to_ping") ; /* Now the only thing i have to do is to look up the '0 received' string into the output to know whether the server is down or up. */ if(strpos($outputShell, '0 received')) { #SERVER is DOWN so i include an error message } else { #SERVER is UP so i include the normal code that is meant to be included by default } ?> I used the shell_exec() function for 2 reasons: 1) the system() function is disabled in my webserver by the hosting company for security reasons 2) the shell_exec() function returns a string containing the output of the shell command. Regards to the community. Hope to have given a hand. Andrea from Italy ---- Manual Page -- http://www.php.net/manual/en/function.shell-exec.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60292 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60292&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60292&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#101112) next »