PHP check website online or offline issue -
this works checking whether website online:
<?php $host = 'http://jdfhgjfdhjanmczxmcnaushdfjsa.com'; if($socket =@ fsockopen($host, 80, $errno, $errstr, 30)) { echo 'true'; fclose($socket); }else{ echo 'false'; }
but when try check multiple sites, not work. return false. why?
<?php $hosts = array(); $hosts[] = 'jdfhgjfdhjanmczxmcnaushdfjsa.com'; $hosts[] = 'google.co.il'; $i = 0; foreach($hosts $host){ $i++; if($socket =@ fsockopen($host[$i], 80, $errno, $errstr, 30)) { echo 'true'; fclose($socket); }else{ echo $host.' false'; } }
the reason 2nd code doesn't work not accessing hosts correctly
change
if($socket = @fsockopen($host[$i]...)
to
if($socket = @fsockopen($host...)
you don't need $i
index. can rid of altogether.
addendum
you noted when first posted calling in loop wasn't working calling 1 domain worked. fix above solution original question. you're facing different problem: missed because added error suppression operator @
in front of function call. removing shows problem:
fsockopen() has been disabled security reasons
online sandboxes disable fsockopen
, cannot rely on output determine whether code works. way it's bad idea use error suppression, particularly in development.
Comments
Post a Comment