Extract Scheme and Host from HTTP_REFERER

后端 未结 4 1718
长情又很酷
长情又很酷 2020-12-11 19:30

I have $_SERVER[\'HTTP_REFERER\'] — pretend it is http://example.com/i/like/turtles.html. What would I need to do to get just the http://example.com

4条回答
  •  一向
    一向 (楼主)
    2020-12-11 20:25

    You could use a regular expression:

    if (isset($_SERVER['HTTP_REFERER']) && preg_match('@^[^/]+://[^/]+@', $_SERVER['HTTP_REFERER'], $match)) {
        var_dump($match[0]);
    }
    

    Or you could use the parse_url function.

提交回复
热议问题