【问题标题】:How to know whether a web link is redirecting to any other domain or staying at the same domain?如何知道一个网络链接是重定向到任何其他域还是停留在同一个域?
【发布时间】:2014-09-17 00:29:39
【问题描述】:

我想知道页面的状态是重定向到任何其他域还是停留在同一个域中。但这对我不起作用。

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

当我打开这个 url 时,它会将我重定向到其他域,我想检测它是停留在 rebelmouse.com 上还是打开任何其他域。

我尝试 curl_init() 来了解状态,但它给出了 200 它没有提供特定的 http 代码来识别重定向。

代码:

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;
    }

【问题讨论】:

    标签: php


    【解决方案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 
    }
    

    查看更多关于here.也许你可以在这里找到你的答案

    【讨论】:

    • 那么您还必须检查重定向标头中的值。
    猜你喜欢
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 2014-08-28
    • 1970-01-01
    • 2019-05-17
    • 2022-10-17
    • 1970-01-01
    • 2019-09-17
    相关资源
    最近更新 更多