Easy way to test a URL for 404 in PHP?
If you are using PHP's curl
bindings, you can check the error code using curl_getinfo
as such:
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
/* Handle 404 here. */
}
curl_close($handle);
/* Handle $response here. */
how to check if a URL exists or not - error 404 ? (using php)
If you have allow_url_fopen
, you can do:
$exists = ($fp = fopen("http://www.faressoft.org/", "r")) !== FALSE;
if ($fp) fclose($fp);
although strictly speaking, this won't return false only for 404 errors. It's possible to use stream contexts to get that information, but a better option is to use the curl extension:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/notfound");
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
$is404 = curl_getinfo($ch, CURLINFO_HTTP_CODE) == 404;
curl_close($ch);
PHP - Check if url is valid or not
The below code works well but when i put urls in array & test the same functionality then it does not give proper results ?
Any thoughts why ?
Also if any body would like to update answer to make it dynamic in the sense (should check multiple url at once, when an array of url provided).
<?php
// URL to check
$url = 'https://www.shareasale.com/m-pr.cfm?merchantID=66802&userID=1860618&productID=1186005518';
$ch = curl_init(); // Initialize a CURL session.
curl_setopt($ch, CURLOPT_URL, $url); // Grab URL and pass it to the variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // Catch output (do NOT print!)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Return follow location true
$html = curl_exec($ch);
$redirectedUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); // Getinfo or redirected URL from effective URL
curl_close($ch); // Close handle
$get_final_url = get_final_url($redirectedUrl);
if($get_final_url){
echo is_url_valid($get_final_url);
}else{
echo $redirectedUrl ? is_url_valid($redirectedUrl) : is_url_valid($url);
}
function is_url_valid($url) {
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_exec($handle);
$httpCode = intval(curl_getinfo($handle, CURLINFO_HTTP_CODE));
curl_close($handle);
echo $httpCode;
if ($httpCode == 200) {
return '<b> Valid link </b>';
}
else {
return '<b> Invalid link </b>';
}
}
function get_final_url($url) {
$ch = curl_init();
if (!$ch) {
return false;
}
$ret = curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_setopt($ch, CURLOPT_HEADER, 1);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$ret = curl_exec($ch);
if (!empty($ret)) {
$info = curl_getinfo($ch);
curl_close($ch);
return false;
if (empty($info['http_code'])) {
return false;
} else {
preg_match('#(https:.*?)\'\)#', $ret, $match);
$final_url = stripslashes($match[1]);
return stripslashes($match[1]);
}
}
}
How to detect PHP script is being used as ErrorDocument for 404 Not Found?
One way to do this would be simply look at the $_SERVER['REQUEST_URI']
and determine if it points at something invalid.
While your example is probably simplified for this question, it might be as simple as
if ($_SERVER['REQUEST_URI'] != 'test.php') {
//we are handling a 404
header("HTTP/1.0 404 Not Found");
echo "These aren't the droids you're looking for";
}
You may need more complex logic, but will all boil down to looking at the request and detecting it was invalid.
As long as the ErrorDocument is a relative URL (doesn't start with http://...), then you should find your script has some extra $_SERVER variables, e.g.
$_SERVER["REDIRECT_URL"]; // /original/path
$_SERVER['REDIRECT_STATUS']; // 404
See the ErrorDocument documentation for more details.
Alternatively, just pass a query string arg to the 404 handler, e.g. ErrorDocument 404 /test.php?error=404
:)
How to create an error 404 page using PHP?
The up-to-date answer (as of PHP 5.4 or newer) for generating 404 pages is to use http_response_code
:
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
die()
is not strictly necessary, but it makes sure that you don't continue the normal execution.
Related Topics
Warning: MySQLi_Select_Db() Expects Exactly 2 Parameters, 1 Given
Parse Youtube Video Id Using Preg_Match
Print Numeric Values to Two Decimal Places
How to Put Composite Keys in Models in Laravel 5
Sorting a PHP Array of Arrays by Custom Order
Check File Extension in Upload Form in PHP
Best Way to Parse Rss/Atom Feeds With PHP
Nginx Location Configuration (Subfolders)
PHPexcel Runs Out of 256, 512 and Also 1024Mb of Ram
How to Get an Unknown Username Given an Id
Split String by New Line Characters
Ajax and PHP to Enter Multiple Forms Input to Database
How to Get the Error Message For the Mail() Function
Pdo Binding Values For MySQL in Statement
Woocommerce Action Hooks and Overriding Templates
How to Create an Array from a CSV File Using PHP and the Fgetcsv Function
PHP Pdoexception: "Sqlstate[Hy093]: Invalid Parameter Number"