Michdd Posted April 19, 2009 Share Posted April 19, 2009 Using a mysql connection and something like.. if(!$connection) works to determine of the server is offline, but that takes a long time (around 10 seconds) is there a way to make it stop trying after say 2 seconds? Link to comment https://forums.phpfreaks.com/topic/154798-a-time-out/ Share on other sites More sharing options...
alphanumetrix Posted April 19, 2009 Share Posted April 19, 2009 Yeah, try using set_time_limit(2); Link to comment https://forums.phpfreaks.com/topic/154798-a-time-out/#findComment-814100 Share on other sites More sharing options...
Michdd Posted April 19, 2009 Author Share Posted April 19, 2009 That won't work, because I need it to do something else, not return an error. I was thinking about something like this: <?php $start = time(); function online($connection) { while(!$connection) { if(time() - $start >= 2) { return false; break; } } return true; } if(online($connection)) { //Your server is online.. } else { //It's not } But it still takes the same amount of time. Link to comment https://forums.phpfreaks.com/topic/154798-a-time-out/#findComment-814121 Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 well, that code wouldn't work anyway. you declared $start outside of the function without making it global: $start = time(); function online($connection) { global $start; put this code at the top of your script: <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; ;?> then put this where you want it: <?php $mtime = microtime(); $mtime = explode(" ",$mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); function online($connection) { global $totaltime; while(!$connection) { if($totaltime >= 2) { return false; break; } } return true; } if(online($connection)) { //Your server is online.. } else { //It's not } ;?> Link to comment https://forums.phpfreaks.com/topic/154798-a-time-out/#findComment-814223 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.