mfindlay Posted April 20, 2007 Share Posted April 20, 2007 Hi, is it possible to detect if a server is up/down and redirect appropriately using php? eg.. //default location $url = 'link1.html'; if ($url == true) echo $url; else echo 'link2.html' //if default location is down use standby I know the code is simplistic but you get the idea. Cheers. Quote Link to comment Share on other sites More sharing options...
clown[NOR] Posted April 20, 2007 Share Posted April 20, 2007 you can try using file_exists() $url = 'link1.html'; if (file_exists($url)) { echo $url; } else { echo 'link2.html'; } Quote Link to comment Share on other sites More sharing options...
Ninjakreborn Posted April 20, 2007 Share Posted April 20, 2007 No, there is not really a way using php that I know of. However there are other things that can be done. For example, I know the owner of PHP freaks has something setup so if any of his site's go down it sends him a page. I don't how this is done, but I know he does it for phpfreaks, and his other site's, so I know there is a way. Quote Link to comment Share on other sites More sharing options...
taith Posted April 20, 2007 Share Posted April 20, 2007 well... sorta... you can detect if the site is there(i have a function for this at home... email me, i'll send it to you later), but you'd have to have some very interesting code to tell wether any webpage is "up" Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 20, 2007 Share Posted April 20, 2007 detect weather your site is making Timeout or not (e.g. set a countdown) if the it responces in that time $res = true; else $res = false; But the Best way to do it is this Step 1: Open a soket on port number 80 on your domain (set time out) Step 2: If you got the Welcome messenge within the Shedule time then the site is Up else its down. Quote Link to comment Share on other sites More sharing options...
mfindlay Posted April 21, 2007 Author Share Posted April 21, 2007 Thanks all, found this on php.net and modified it, does the trick..pings the server - no idea how it works!! <?php class Net_Ping { var $icmp_socket; var $request; var $request_len; var $reply; var $errstr; var $time; var $timer_start_time; function Net_Ping() { $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1); socket_set_block($this->icmp_socket); } function ip_checksum($data) { for($i=0;$i<strlen($data);$i += 2) { if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]); else $bits = unpack('C*',$data[$i]); $sum += $bits[1]; } while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16); $checksum = pack('n1',~$sum); return $checksum; } function start_time() { $this->timer_start_time = microtime(); } function get_time($acc=2) { // format start time $start_time = explode (" ", $this->timer_start_time); $start_time = $start_time[1] + $start_time[0]; // get and format end time $end_time = explode (" ", microtime()); $end_time = $end_time[1] + $end_time[0]; return number_format ($end_time - $start_time, $acc); } function Build_Packet() { $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data $type = "\x08"; // 8 echo message; 0 echo reply message $code = "\x00"; // always 0 for this program $chksm = "\x00\x00"; // generate checksum for icmp request $id = "\x00\x00"; // we will have to work with this later $sqn = "\x00\x00"; // we will have to work with this later // now we need to change the checksum to the real checksum $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data); // now lets build the actual icmp packet $this->request = $type.$code.$chksm.$id.$sqn.$data; $this->request_len = strlen($this->request); } function Ping($dst_addr,$timeout=5,$percision=3) { // lets catch dumb people if ((int)$timeout <= 0) $timeout=5; if ((int)$percision <= 0) $percision=3; // set the timeout socket_set_option($this->icmp_socket, SOL_SOCKET, // socket level SO_RCVTIMEO, // timeout option array( "sec"=>$timeout, // Timeout in seconds "usec"=>0 // I assume timeout in microseconds ) ); if ($dst_addr) { if (@socket_connect($this->icmp_socket, $dst_addr, NULL)) { } else { $this->errstr = "Cannot connect to $dst_addr"; return FALSE; } $this->Build_Packet(); $this->start_time(); socket_write($this->icmp_socket, $this->request, $this->request_len); if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0)) { $this->time = $this->get_time($percision); return $this->time; } else { $this->errstr = "Timed out"; return FALSE; } } else { $this->errstr = "Destination address not specified"; return FALSE; } } } $ping = new Net_Ping; $ping->ping("app.dll"); if ($ping->time) $url 'app1.dll'; else $url 'app2.dll'; ?> Cheers Quote Link to comment Share on other sites More sharing options...
mfindlay Posted April 25, 2007 Author Share Posted April 25, 2007 Hello again, one of our IT guys gave me this tip, if using a unix/apache set-up just use unix inbuilt ping function, so instead of the half page script above, we end up with: $ip="anysite.com"; $timeout=2; exec("/usr/sbin/ping $ip $timeout", $output, $return); if ($return===0) { echo "ok\n"; } else { echo "not ok\n"; } Cheers Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 25, 2007 Share Posted April 25, 2007 Its Funny and Impossible. If anysite.com is not at all accessible How can anysite.com/page.php Can Run that above code to check weather the server is running or not. And in your code you are checking weather the server is on or Off. Its likea blind man choosing color of a shirt. there is 2 ways to do this. detect weather your site is making Timeout or not (e.g. set a countdown) if the it responces in that time $res = true; else $res = false; But the Best way to do it is this Step 1: Open a soket on port number 80 on your domain (set time out) Step 2: If you got the Welcome messenge within the Shedule time then the site is Up else its down. here also if the server is down you can check but if the Server is OFF you cant check. Quote Link to comment Share on other sites More sharing options...
trq Posted April 25, 2007 Share Posted April 25, 2007 if using a unix/apache set-up just use unix inbuilt ping function ping exists in windows too. if anysite.com is not at all accessible How can anysite.com/page.php Can Run that above code to check weather the server is running or not. Obviously you don't run the script from the server you are testing. Quote Link to comment Share on other sites More sharing options...
mfindlay Posted April 25, 2007 Author Share Posted April 25, 2007 Hi neel_basu, I am not trying to see if my site is up/down, this is a replacement login script for an assessment system which runs on 2 live windows servers (default and standby), so what the login page (running on an apache server) is doing, is deciding which server to login to. The IT guys have the Apache server set up so normal users (me!) can't do things like open sockets and the like. The first script I posted worked fine on my test server on my local machine and failed on the live server hence the unix ping suggestion. Another (maybe better??) alternative would be to extract the headers from the page(s) i am calling which would test whether the page rather than the server was up/down, but the assessment app has app.dll as an extension so it has to run to generate the headers, so I guess testing whether the server is running is as good as I can get. Maybe this explains a bit better, although I am always open to suggestions... Cheers Quote Link to comment Share on other sites More sharing options...
neel_basu Posted April 25, 2007 Share Posted April 25, 2007 Ok You can do it to test other server(s). Ping would work in both Unix and Windows exec("ping $ip); On windows would work. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.