我正在使用这样的代码从入站链接中获取 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://
一如既往地提前致谢。
最佳答案
当然可以。一些简单的字符串操作和替换应该是您从任何 URL 中删除 www
所需的全部 -
$inbound_url = str_replace('http://www','http://',$inbound_url);
定义见the documentation -
str_replace()
- Replace all occurrences of the search string with the replacement string
请注意,我在搜索中包括了 http://
,这样任何其他出现的字符串 www
URL 都将保持不变。
关于php - 使用 $_SERVER ['HTTP_REFERER' ];,但修改为 www,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14367304/