Reputation: 31
What I want to see is whether the url entered by the user redirects to another page in PHP. I do not want to see where it redirects to, but just whether it redirects to another page or not.
Here's what I'm currently trying to use:
<?php
echo "Redirect check";
$ch = curl_init('http://www.google.com/');
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code === 301 || $code === 302 || $code === 303 || $code === 307) {
$redirect = true;
}else{
$redirect = false;
}
?>
However, when I run this, it redirects me to yahoo.com after showing "redirect check" for a few moments.
Any help would be appreciated.
Thanks in advance!
Upvotes: 0
Views: 966
Reputation: 57650
You dont need the BODY when you are checking only the headers. Just send a HEAD
request. Also you dont need to redirect. So add the following options before curl_exec
.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
See example
Upvotes: 1