1

I am trying to know the status of a page whether the page is redirecting to any other domain or staying in the same domain. But this is not working for me.

https://www.rebelmouse.com/rohit/612798926.html

when i open this url it redirects me to other domain and i want to detect whether it stays on rebelmouse.com or opens any other domain.

I have tried curl_init() to know the status but it gives 200 it does not provide specific http code to identify redirection.

CODE:

function file_get_contents_curl($url) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
        curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

        $data = curl_exec($ch);

        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $urlInfo = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

        curl_close($ch);
        echo $urlInfo;
        return $data;
    }

1 Answers1

1
$ch = curl_init('url_here/');
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (($code == 301) || ($code == 302)) { //301 and 302 specifies the redirects.
//your stuff 
}

Check more about here.Maybe you can find ur answer here

Community
  • 1
  • 1
Avinash Babu
  • 6,171
  • 3
  • 21
  • 26