Solved

Checking links for 401, 404 200 etc...

Posted on 2004-09-09
6
237 Views
Last Modified: 2008-03-17
Hello.

I use the following to check to see if a link is valid:
----------------------------------------------------
      $fh=@fopen(dirname($_SERVER["SCRIPT_URI"])."/".$filename, "r");
                  if($fh){
                  $contentout.='<font color="green">LINK OK</font>';
                  fclose($fh);
            }
            else
                  $contentout.='<font color="red">LINK FAILED</font>';
            }
----------------------------------------------------

It does not work for links that require authorization (401 Authorization Required)

How can I get it to say LINK OK if it can access the link, LINK FAILED if it is a bad link and PASSWORD REQUIRED if a password is required?

Thanks!
0
Comment
Question by:hankknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:aratani
ID: 12020657
This is a better script to see if the link is valid. It was on the PHP documentation pages. However, it is not possible to look at the header of the documents returned and check for whether the pages are 401 authorization required (since the pages that require authorization also return as 200 OK).  

A way around it would be to go through the contents of the page and check for the words authorization required.  If you want a script for that, please tell me. But then good pages that just have the string "authorization required" would also be a part of the results.

function url_validate( $link )
{        
       $url_parts = @parse_url( $link );

       if ( empty( $url_parts["host"] ) ) return( false );

       if ( !empty( $url_parts["path"] ) )
       {
                   $documentpath = $url_parts["path"];
       }
       else
       {
                   $documentpath = "/";
       }

       if ( !empty( $url_parts["query"] ) )
       {
                   $documentpath .= "?" . $url_parts["query"];
       }

       $host = $url_parts["host"];
       $port = $url_parts["port"];
       // Now (HTTP-)GET $documentpath at $host";

       if (empty( $port ) ) $port = "80";
       $socket = @fsockopen( $host, $port, $errno, $errstr, 30 );

       if (!$socket)
       {
                   echo "bad link";
                   return(false);
       }
       else
       {
             fwrite ($socket, "HEAD ".$documentpath." HTTP/1.1\r\nHost: $host\r\nConnection: close\r\n\r\n");
             $http_response = fgets( $socket, 22 );
                  
             if ( ereg("200 OK", $http_response, $regs ) )
             {
                        echo "Good link<br>";
                                                                echo "HTTP-Response: $http_response<br>";
                         return(true);
                         fclose( $socket );
             }

             else
             {
                                                   echo "Bad link<br>";
                                                   echo "HTTP-Response: $http_response<br>";
                     return(false);
             }
       }
}
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12020700
If you turn on track_errors in php.ini, then the error header will appear in $php_errormsg.  You can then use any of the string search functions to look for 401, 404, etc.
0
 
LVL 4

Expert Comment

by:aratani
ID: 12020729
So, there will be a global variable called $php_errormsg that you can use or how do you access $php_errormsg?

Thanks

AJ
0
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 33

Accepted Solution

by:
snoyes_jw earned 500 total points
ID: 12020965
I don't know if it is global, but it will be in local scope at least.  So you can do this:

$fh=@fopen(dirname($_SERVER["SCRIPT_URI"])."/".$filename, "r");
if($fh){
  $contentout.='<font color="green">LINK OK</font>';
  fclose($fh);
}
else
  if (strstr($php_errormsg, "401"))
    $contentout .= '<font color="red">PASSWORD REQUIRED</font>';
  if (strstr($php_errormsg, "404"))
    $contentout.='<font color="red">LINK FAILED</font>';
}
0
 
LVL 16

Author Comment

by:hankknight
ID: 12026419
Thanks.

If I turn on track_errors in php.ini what kind of effect would it have on the performance of all my other scripts?

And how many kinds of errors does it track?
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 12026778
http://www.php.net/manual/tw/reserved.variables.php#reserved.variables.phperrormsg

I suspect performance issues will be virtually nil, unless you were in the habit of using variables named $php_errormsg for your own.  Documentation simply says, "the last error message generated by PHP", and I know that it includes warnings.  I don't know what effect the various error reporting options have.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question