Solved

Checking links for 401, 404 200 etc...

Posted on 2004-09-09
6
221 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now