Solved

Checking links for 401, 404 200 etc...

Posted on 2004-09-09
6
224 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

777 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