1

Okay so I'm trying to create a server status page on my website and I've been using the below function to see if a website is live or not. however when I try to do more than one search the response is always DOWN for the responses after the first one.

Visit Function

function Visit($url){
       $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init();
       curl_setopt ($ch, CURLOPT_URL,$url );
       curl_setopt($ch, CURLOPT_USERAGENT, $agent);
       curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
       curl_setopt ($ch,CURLOPT_VERBOSE,false);
       curl_setopt($ch, CURLOPT_TIMEOUT, 5);
       curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
       curl_setopt($ch,CURLOPT_SSLVERSION,3);
       curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);
       $page=curl_exec($ch);
       $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
       curl_close($ch);
       if($httpcode>=200 && $httpcode<300) return true;
       else return false;
}

index.php

if (Visit("http://twitter.com")){
       echo "OK";
}else{
       echo "DOWN";
}

if (Visit("http://google.com")){
       echo "OK";
       }else{
       echo "DOWN";
}

Error Log

[30-Jun-2016 12:11:10 UTC] PHP Warning:  fread() expects parameter 1 to be resource, boolean given in /public_html/blog/server-status.php on line 40
[30-Jun-2016 12:11:10 UTC] PHP Warning:  fclose() expects parameter 1 to be resource, boolean given in /public_html/blog/server-status.php on line 41

The 40th and 41st lines are;

   curl_setopt($ch, CURLOPT_USERAGENT, $agent);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

The response given when executing the above scripts is OK DOWN even though google is up. I have tried with other links and all produce the same error.

  • Are you getting any errors? Are errors even on? Have you checked what code you're getting back? What attempts have you made to solve this so far? – Styphon Jun 30 '16 at 12:30
  • 1
    Sorry yes I have just edited with the error log. I really don't know if the error log is relevant as it doesn't seem to be up to date but I just pulled the latest logs in the error_log file. –  Jun 30 '16 at 12:32
  • 1
    Take a look at [**How can I check if a URL exists via PHP?**](http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php) – FirstOne Jun 30 '16 at 12:40
  • Your error log and the lines you show seem unrelated. There is no calls to fread or fclose in those lines. – Gordon Jun 30 '16 at 12:47

1 Answers1

1

The reason why the function does not return true, it's because Google answers with a 302 Found HTTP code.

Google Inspector

Same thing for Twitter, but it answers with a 301 instead.

You should check if the response was a redirect or not, and maybe return it accordingly.

This is what var_dump($httpcode) outputs;

enter image description here

I have attempted using Visit("https://www.google.it") and it works as it should.

enter image description here

GiamPy
  • 3,543
  • 3
  • 30
  • 51
  • 1
    Okay yeah that does make sense, so I just have to find out what http code my site is producing when its live and not live and then alter my code to read that. Thankyou. –  Jun 30 '16 at 12:39
  • 1
    @GiamPy did you try using the function more than once in the same document for two different urls –  Jun 30 '16 at 12:40
  • Yes @LewisDay. http://puu.sh/pLnh2/58d976fb6b.png I tried `https://www.netflix.com/it/` and it works. You just need to be careful because 70% of the websites do redirects to the correct routes when you visit their index. Most have localization, some use HTTPS, some use WWW, etc. – GiamPy Jun 30 '16 at 12:42