doulian4467 2017-03-20 10:37
浏览 179
已采纳

如何检查访问者重定向的链接

I want to check the source of each visitors and store this information in the cookies.

The idea is: when someone goes to google and search for i.e Wikipedia and clicks on the first result this is what google generates

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&sqi=2&ved=0ahUKEwiB8aaX8eTSAhWCNJoKHbYqDFsQFggnMAE&url=https%3A%2F%2Fwww.wikipedia.org%2F&usg=AFQjCNFoqWBYY9IYRUE6eu9l7l73Wk5wYw&bvm=bv.149760088,d.bGs

I don't know if it is possible in PHP to echo "Google.com" as the source of this redirection.

  • 写回答

2条回答 默认 最新

  • douhui5953 2017-03-20 10:40
    关注

    $_SERVER['HTTP_REFERER'] will give you the referrer page's URL if there exists any. In case of direct access (from a bookmark or user entering the url directly) you won't get anything.

    Using HTTP_REFERER in PHP

    The HTTP REFERER in PHP is stored in the $_SERVER super global, and can be referenced from anywhere in your PHP code like in the following example, which would simply write it out to the browser:

    echo $_SERVER['HTTP_REFERER'];
    

    If the HTTP_REFERER has been set then it will be displayed. If it is not then you won't see anything. If it's not set and you have error reporting set to show notices, you'll see an error like this instead:

    Notice: Undefined index: HTTP_REFERER in /path/to/filename.php on line 3 To prevent this error when notices are on (I always develop with notices on), you can do this:

    if(isset($_SERVER['HTTP_REFERER']))
    {
        echo $_SERVER['HTTP_REFERER'];
    }
    

    In order to get the domain form the referrer you can use parse_url() function of PHP: Example:

    $url = '//www.example.com/path?name=shakti';
    var_dump(parse_url($url));
    

    You will get an output as:

    array(3) {
      ["host"]=>
      string(15) "www.example.com"
      ["path"]=>
      string(5) "/path"
      ["query"]=>
      string(17) "name=shakti"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记