Shannid1987
Shannid1987

Reputation:

How to check whethera link is up or not?

I want to create a webpage in which I am adding some intranet links. I just wanted to know how to check whther the link that i have added, at a moment is working or not. I want to mark the link in RED if its not working else it should be green.

Upvotes: 1

Views: 367

Answers (4)

Lance Rushing
Lance Rushing

Reputation: 7640

If you want check sever side (php example):

<?php

function isup($url) 
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    if(curl_exec($ch)) {
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        return ($httpcode < 400);
    }

    return false;
}

$url = "http://example.com/404";

printf('<a href="%s" class="%s">Link</a>', $url, isup($url) ? 'good' : 'bad');

Upvotes: 0

Lance Rushing
Lance Rushing

Reputation: 7640

If you want to check client side...

<style type="text/css">
.good {
   color:green;
}

.bad {
   color:red;
}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>

<script type="text/javascript">
    $(document).ready(function(){
        $("a.checkLinks").each(checkme);
    })

    // load page from URL with ajax
    function checkme(i, item) {
        $.ajax({
            type: "GET",
            url: item.href,
            success: function(data, textStatus) {
                $(item).addClass("good");
            },
            error: function(request) {
                $(item).addClass("bad");
            }
          });
    }
</script>

<a class="checkLinks" href="good.html">Good</a><br/>
<a class="checkLinks" href="bad.html">Bad</a>

Upvotes: 0

Stef
Stef

Reputation: 6991

Ping something on the other side of the link. While you could probably find ways to check interface status or routing tables or various other things ... what you really want to know is whether the traffic is flowing to and from that destination.

On unix can use ping with the -c argument to send a number of packets, and -w to specify the wait time in seconds. Then check the exit status:

ping -c 3 -w 5 remote-host

ping will exit with non-zero exit code if packets are dropped.

Upvotes: -2

Asaph
Asaph

Reputation: 162821

If you want a lightweight programmatic check, you could do an HTTP HEAD request and check for a response code greater than or equal to 200 and less than 400.

Upvotes: 3

Related Questions