We help IT Professionals succeed at work.
Get Started

Testing URL

nicey asked
Last Modified: 2008-08-15

I'm trying to test that a given URL is active on the Internet.

I'm using the following (before this though, i'm using Regular Expresssions to first check that the URL "looks" valid) :

function testURL($url) {

  $urlArray = parse_url($url);
  if (isset($urlArray['query'])) {
   $urlArray['query'] = "?" . $urlArray['query'];
  } else {
   $urlArray['query'] = "";

  $header = "GET " . $urlArray['scheme'] . "://" . $urlArray['path'] . $urlArray['query'] . " HTTP/1.0\r\n\r\n";

  $gotInfo = false;
  $found = false;
  if (($fp = @fsockopen($urlArray['host'], 80, $errno, $errstr, 5))) {
   if (fputs ($fp, $header)) {
    if (($snippet = fgets ($fp, 1024)) && ($snippet != "")) {
     $gotInfo = true;

  if ($gotInfo) {
   if ((stristr($snippet, '200 OK')) || (stristr($snippet, '302 Found')) || (stristr($snippet, '304 Not Modified'))) {
    $found = true;

  return $found;

And it seems ok, most of the time.

Only, some websites return Error 404 pages, even though the given URL is valid if you put it into a browser address bar.

Also, if a custom Error 404 is employed by a site, this function returns "true", as I assume that upon the server redirecting the target page sends our a 200 OK header?

Also, somtimes when you give it what is clearly a non existent domain, the function returns "true". Put the non-existent URL into a browser, and its shows "Page Cannot Be Displayed".

What way is there to test that a page is valid, without these problems?
Watch Question
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE