Jump to content

Detect if website is down


JohnnyDoomo

Recommended Posts

I was looking for code to create a site that allows you to insert a domain and check if the site is up or not from the servers location. I found a tutorial that contained the code below, but one problem I had when I tested it on a site I knew was down, is that the script hanged for a very long time.

 

Is what I need in the code below is something of a time out limit and if the script can't perform the action of detecting if the site is up or down within a certain timeframe, then assume the site is down, and display the "down" message.

 

I'm not a programmer, so though it's probably something simple, I have no idea how to do it.

 

 

 

<?php

    if (isset($_POST['submit'])) {

        $url = $_POST['url'];

        $curl = curl_init($url);

        curl_setopt($curl, CURLOPT_NOBODY, true);

        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

        curl_exec($curl);

        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        if ($code == 200) {

            echo "<h3>Up!</h3>";

        } else {

            echo "<h3>Down!</h3>";

        }

    }

?>

 

<form action="" method="post">

    Url: <input type="text" name="url" /><br />

    <input type="submit" name="submit" />

</form>

 

On a side note, if it's not too much trouble to add, I would like the script to be able to process the url no matter how it's entered. So whether http://google.com or www.google.com or http://www.google.com or http://www.google.com/blah it is smart enough to trim any of that down to just google.com or however the script needs it to check it.

 

I don't really understand what the above code is doing, so if somebody could explain it, that would help me understand more about what it's doing to check if the website is up or down. I had one script that was only pinging the server, which I quickly found wasn't an accurate way of telling if a website was up or not from the servers location.

 

Thanks for any help I can get on this.

Link to comment
Share on other sites

You can use fsockopen to test if a server is online, just change the timeout to the number of seconds you wish the server to respond. To get the hostname out of an URL you can use parse_url with second parameter PHP_URL_HOST.

 

Unfortunately I'm not much of a programmer and I don't know how to use those commands. Is it at all possible to get what you're talking about written out in code that I can test?

 

I don't know how fsockopen() compares to what I originally posted, I'm just looking for whatever method would be the simplest, most accurate, least resource using method of detecting if a site is up or not from my servers location to the server inputted by a user. I don't know php though, so I'm unable to write much of any code myself.

Link to comment
Share on other sites

Im not sure, as I have never used this before, but something like this might work?

$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "website not found";
} else {
    echo "website found";
}
 
Edited by drewdan
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.