使用$ _SERVER ['HTTP_REFERER'];,但修改为www

时间:2013-01-16 20:33:24

标签: php html mysql

我正在使用这样的代码来从入站链接中获取URL:

$inbound_url = $_SERVER['HTTP_REFERER'];

//then do some stuff writing the url to a database table, but....
//ONLY IF the url doesn't already exist in the table

假设该链接来自同一网站,同一网页,但仅在www中有所不同。所以我明白了: 1)http://www.mysite.com/page.html 2)http://mysite.com/page.html

这会在我的表格中显示两次,因为其中一个有www而另一个没有。

有没有办法解析$ _SERVER ['HTTP_REFERER']的结果;要么: 1)添加www。它失踪的地方,或者 2)剥离一切... http://...www。或..http://

一如既往地感谢您。

2 个答案:

答案 0 :(得分:2)

当然可以。您应该从一个URL中删除www所需的一些简单的字符串操作和替换 -

$inbound_url = str_replace('http://www','http://',$inbound_url);

the documentation -

中所定义
  

str_replace() - 用替换字符串

替换所有出现的搜索字符串

请注意,我在搜索中包含http://,以便字符串www网址的任何其他匹配项都保持不变。

答案 1 :(得分:1)

使用此

$url = 'http://stackoverflow.com';
$d = array_shift( explode( '.', str_replace('www.', '', parse_url( $url, PHP_URL_HOST )) ) );
echo $d; //stackoverflow

或者您也可以使用

http://php.net/manual/en/function.parse-url.php功能