Check if Website is Available
Performs a cURL-Request to check, if a website exists / is online
Technique #1
<?php
if (isDomainAvailible('http://www.css-tricks.com'))
{
echo "Up and running!";
}
else
{
echo "Woops, nothing found there.";
}
//returns true, if domain is availible, false if not
function isDomainAvailible($domain)
{
//check, if a valid url is provided
if(!filter_var($domain, FILTER_VALIDATE_URL))
{
return false;
}
//initialize curl
$curlInit = curl_init($domain);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
//get answer
$response = curl_exec($curlInit);
curl_close($curlInit);
if ($response) return true;
return false;
}
?>
Technique #2
<?php
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);
//echo curl_error($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300) return true;
else return false;
}
if (Visit("http://www.google.com"))
echo "Website OK"."n";
else
echo "Website DOWN";
?>
Technique #3
<?php
ini_set("default_socket_timeout","05");
set_time_limit(5);
$f=fopen("http://www.css-tricks.com","r");
$r=fread($f,1000);
fclose($f);
if(strlen($r)>1) {
echo("<span class='online'>Online</span>");
}
else {
echo("<span class='offline'>Offline</span>");
}
?>
In technique #2 it should probably read
... && $httpcode<400) return true;instead of
... && $httpcode<300) return true;Otherwise a redirect would be regarded as “server down”.
Hello NetHawk.
I tested and you are totally right. $httpcode<300 always returns server down.
Anyway, I made a little modification so I can test several sites, but sometimes, sites appear down randomly, what could be wrong if you can help me? I tried to do this for a cron job.
CODE:
=200 && $httpcode”.$sitio.”";
else
echo “DOWN->”.$sitio.”";
}
?>
It seems like code was cutted… Anyway, the only modification I made, was put urls in an array, then test one by one in a foreach loop.
how to use it ?????????
what is the method
Copy code from example #2 into a text file. Give it the extension .php. Move the file to your webserver and call the url that points to the file. If you have a PHP enabled server (most are) the code will check if Google is up (probably always).
You can take it from there, but you will need some basic PHP knowledge.
Which technique is better to use? advantages? disadvantages?
#1 checks if the url is valid, that’s a great plus, if the urls are entered by a user and don’t come out of a database or from a list with urls known to be valid.
#2 on the other hand submits a user agent string. Without it, some server will answer the request with a error 403 (forbidden) or 405 (method not allowed).
Conclusion: I suggest #2 but with the filter test of #1.
Actually, Chris should say something about this, because he is running aremysitesup.com, a service that probably uses this technique (see button in the footer of this page – great service, highly recommended).
We actually use Ruby and some methods that are far more complex =). But the principal is the same.
Thanks! I’m wondering if http://www.checksite.us uses the same method?
Also, what is the purpose of the agent?
Checksite.us is probably based on a similar mechanism, although it’s not clear what language is used.
The UserAgent is a string sent by any browser to identify itself. At least, that was the intention. Nowadays many browser lie about the details. However, as long as a UserAgent is sent, the web server usually sees a request as legitimate. Also robots, e.g. Google have UserAgent strings to identify them.
If no UserAgent string is sent, some web server don’t let you access the site for whatever reason (they probably don’t like the idea that some tool or other server accesses their pages directly).
You can see the UserAgent string of your browser (among other information) on this page:
http://browser.delucamarketing.ch/
none of these techniques really seem to work…everytime i check something like http://www.adfsfdasffsfasfsf.com it always comes back as 200…does anyone know of a way to find out if a website is unavailable like *cough* *cough* godaddy *cough* *cough*
It is a simple whois look up. You need to create a socket connection on port 43. It is just getting PHP to connect to the exact same whois server terminal uses when you do a whois lookup.
It is a simple whois look up. You need to create a socket connection on port 43. It is just getting PHP to connect to the exact same whois server terminal uses when you do a whois lookup.
Sorry for some reason it is not showing the code, you can find a good tut at http://www.phptoys.com/e107_plugins/content/content.php?content.38 which details how to build one.
There’s another solution that uses sockets:
http://neo22s.com/check-if-url-exists-and-is-online-php/
Hi Chris,
Any idea, how can I check if a bunch of URLS are valid. For example. I need to run a check on 1000 twitter profiles and I need to know if they are active or invalid URLS. I want to be able to paste the entire list of twitter urls and check if they are valid pages.
Nice,
But is there any API from who.is to check it? I think using web services will help the cause more effectively. Say as I did in getting the alexa ranking,
http://tutology.net/category/how-php/get-alexa-rank-php-and-alexa-api
Great concepts
Seems to me that these scripts wouldn’t actually work if there was a redirect. So “facebook.com” and “www.facebook.com” might return different results even if the site was up.
No?
Anyone know how “downforeveryoneorjustme.com” does it? That script seems to work every time, redirects or not…
Those codes are not bulletproof. I tried different domains at isitdownrightnow.com and the script fails on some domains. ask.com is such an example.
http://www.downforeveryoneorjustme.com/ask.com
However it works at Doj.me.. Not sure what method they are using…
http://doj.me/?url=ask.com
Hello,
Thanks for the tuts. But, one question though. How do you make sure that it is not your internet connection that has disconnected?
Thanks a lot.
You wouldn’t have even been able to connect to the server to run the script if this was the case : )
Hello
None of these scripts work for me!
I am trying to get the script to tell me if my server is online. I have no domain, so I’m using the IP (and somtimes a port).
When i have entered the IP to my server, it says it’s online.
Also; when I am shutting down the server, it still says it’s online…
How can I fix this? Any ideas?
– Mystic
That’s a good technique and a very similar technique is posted here : http://geeks-wiki.blogspot.com/2012/10/how-to-check-if-url-is-up.html , i think this technique is similar yet effective.
Best,
Patrick