Solved

Checking links for 401, 404 200 etc...

Posted on 2004-09-09
6
218 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
Comment Utility
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
Comment Utility
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
Comment Utility
So, there will be a global variable called $php_errormsg that you can use or how do you access $php_errormsg?

Thanks

AJ
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 33

Accepted Solution

by:
snoyes_jw earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses four methods for overlaying images in a container on a web page
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 dynamically set the form action using jQuery.

772 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