Solved

Checking links for 401, 404 200 etc...

Posted on 2004-09-09
6
234 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

740 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