Let's assume my URL is mysite.com/myuri. There are many methods the world knows, to know if a local uri myuri exists on my site mysite.com.
$uri_exists = file_exists($_SERVER['DOCUMENT_ROOT']."/myuri")
.$headers = @get_headers("http://mysite.com/myuri");
$uri_exists = ($headers[0] == "HTTP/1.1 404 Not Found");
$curl = curl_init('http://mysite.com/myuri');
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);
$info = curl_getinfo($curl);
$uri_exists = ($info['http_code'] == 200);
curl_close($curl);
These were the methods I am aware of. The first method precisely checks if a file exists, not if a URI exists. If mod_rewrite
is used, this method is highly inaccurate.
The next two methods are accurate (even when mod_rewrite
is used) but slow since they perform remote GET requests to their own site. They are also inefficient because, just to know if a URI exists the scripts on mysite.com will execute themselves unnecessarily. Moreover they will cause unnecessary traffic to mysql server.
Coming to what I am trying to ask, I want the PHP scripts at mysite.com to check if a URL mysite.com/myuri exists using a method more efficient and accurate than the above three methods.
Thank you
Peace...
Instead of sending a GET
request, which will result in having the contents of the uri returned to you, you could use a HEAD
request, which, depending on the web application implementation, may just tell you some information about the uri instead of trying to return it.
You could modify your third example using the following code:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
If you are using PHP and Apache, depending on the SAPI you might be able to use apache_lookup_uri($uri) which may be slightly more efficient.