Posted by Shadowhand on Thu 31 Jan 15:44 (modification of post by view diff)
report abuse | download | new post
- <?php
- /**
- * is_file_url() check if a URL gives a valid response code
- *
- * @param STRING $url - The URL to check (needs scheme and host, must be http only)
- * @param BOOL $allowRedirects - If a 3xx code is returned and a location header is given, should we follow it?
- * @param INT $maxNumRedirs - How many redirects should we follow befroe bailing out?
- * @param INT $redirNum - Do NOT Set this in your calls to the function--used to track recursive calls and avoid endless recursion
- * @return BOOL - Whether or not the URL returned a valid response code
- */
- function is_file_url($url,$allowRedirects=true,$maxNumRedirs=5,$redirNum=0)
- {
- $returnValue = false;
- if($redirNum<=$maxNumRedirs)
- {
- $check = httpRequestStatus($url);
- if($check[0]==200)
- {
- $returnValue = true;
- }
- {
- $returnValue = is_file_url($check[1],$allowRedirects,$maxNumRedirs,($redirNum+1));
- }
- }
- return $returnValue;
- }
- function httpRequestStatus($url){
- if($urlParts['scheme']=='http' &&
- preg_match("/^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/",$urlParts['host'])
- )
- )
- {
- $httpREQ = "HEAD {$urlParts['path']} HTTP/1.0\r\nHost: {$urlParts['host']}\r\n\r\n";
- $returnFromServer = '';
- break;
- }
- }
- $statusCode = $statusCode[0];
- $locationField = (
- )
- ? $parsedLocationField[1]
- : '';
- }
- }
- }
- }
- return $returnValue;
- }
- //Examples:
- //Valid URL, is_file_url() returns true
- $url = 'http://www.jaaulde.com/';
- //Invalid URL, is_file_url() returns false
- $url = 'http://www.jaaulde.com/feuhfiuhufih';
- //Valid URL, that has redirection, when allowing redirection is_file_url() returns true
- $url = 'http://www.jaaulde.com/test_bed/cookieLib';
- //Valid URL, that has redirection, when NOT allowing redirection is_file_url() returns false
- $url = 'http://www.jaaulde.com/test_bed/cookieLib';
- //Sample output when run from command line:
- /*
- Jims-MacBook-Pro:~ jim$ php isfileurl.php
- http://www.jaaulde.com/ is good
- http://www.jaaulde.com/feuhfiuhufih is bad
- http://www.jaaulde.com/test_bed/cookieLib is good
- http://www.jaaulde.com/test_bed/cookieLib is bad
- Jims-MacBook-Pro:~ jim$
- */
- ?>
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.