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.
- The easiest and the fastest method is:
$uri_exists = file_exists($_SERVER['DOCUMENT_ROOT']."/myuri")
. - The next method is:
$headers = @get_headers("http://mysite.com/myuri");
$uri_exists = ($headers[0] == "HTTP/1.1 404 Not Found");
- Another (a little faster than the previous one) method is:
$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...