How to Check If a Url Exists Via PHP

How can I check if a URL exists via PHP?

Here:

$file = 'http://www.example.com/somefile.jpg';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}

From here and right below the above post, there's a curl solution:

function url_exists($url) {
return curl_init($url) !== false;
}

check if url exists php

I would use cURL for url verification. An example method would be as follows

    public function urlExists($url) {

$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec($handle);
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);

if($httpCode >= 200 && $httpCode <= 400) {
return true;
} else {
return false;
}
}

How to check a url exist or not in php

if(! @ file_get_contents('http://www.domain.com/file.txt')){
echo 'path doesn't exist';
}

This is the easiest way to do it. If you are unfamiliar with the @, that will instruct the function to return false if it would have otherwise thrown an error

PHP - Check if the final URL exists

You can tell cURL to follow all redirects, and return the result from the final redirection. Use:

curl_setopt($handle, CURLOPT_FOLLOWLOCATION, true);

What is the best way to check if a URL exists in PHP?

You can use get_headers($url)

Example 2 from Manual:

<?php
// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
array(
'http' => array(
'method' => 'HEAD'
)
)
);
print_r(get_headers('http://example.com'));

// gives
Array
(
[0] => HTTP/1.1 200 OK
[Date] => Sat, 29 May 2004 12:28:14 GMT
[Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux)
[Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT
[ETag] => "3f80f-1b6-3e1cb03b"
[Accept-Ranges] => bytes
[Content-Length] => 438
[Connection] => close
[Content-Type] => text/html
)

The first array element will contain the HTTP Response Status code. You have to parse that.

Note that the get_headers function in the example will issue an HTTP HEAD request, which means it will not fetch the body of the URL. This is more efficient than using a GET request which will also return the body.

Also note that by setting a default context, any subsequent calls using an http stream context, will now issue HEAD requests. So make sure to reset the default context to use GET again when done.

PHP also provides the variable $http_response_header

The $http_response_header array is similar to the get_headers() function. When using the HTTP wrapper, $http_response_header will be populated with the HTTP response headers. $http_response_header will be created in the local scope.

If you want to download the content of a remote resource, you don't want to do two requests (one to see if the resource exists and one to fetch it), but just one. In that case, use something like file_get_contents to fetch the content and then inspect the headers from the variable.

PHP check if url parameter exists

Use isset()

$matchFound = (isset($_GET["id"]) && trim($_GET["id"]) == 'link1');
$slide = $matchFound ? trim($_GET["id"]) : '';

EDIT:
This is added for the completeness sake. $_GET in php is a reserved variable that is an associative array. Hence, you could also make use of 'array_key_exists(mixed $key, array $array)'. It will return a boolean that the key is found or not. So, the following also will be okay.

$matchFound = (array_key_exists("id", $_GET)) && trim($_GET["id"]) == 'link1');
$slide = $matchFound ? trim($_GET["id"]) : '';

Check if $_REQUEST url exists in database

You can check the number of results with $ArticleSQL->num_rows.

So your code will now be:

$ArticleSQL = $mysqli->query("SELECT * FROM articles WHERE id='$id' ");

if($ArticleSQL->num_rows > 0) {
$article = $ArticleSQL->fetch_array();

//show the article here

} else {

//redirection:
header("Location: index.php");
}


Related Topics



Leave a reply



Submit