删除 PHP 中的查询字符串(有时基于引用者)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4270677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing query string in PHP (sometimes based on referrer)
提问by Cameron
Hi on Facebook the home link has a query string on it like this: facebook.com/?ref=homewhen you click the link and navigate to the home page the query is automatically removed. However if I was to manually type in that link the query is NOTremoved. Any idea on how they did this?
嗨,在 Facebook 上,主页链接上有一个查询字符串,如下所示:facebook.com/?ref=home当您单击链接并导航到主页时,查询将自动删除。但是,如果我要手动输入该链接,则不会删除查询。知道他们是如何做到的吗?
回答by netcoder
Easiest way in PHP:
PHP 中最简单的方法:
$url = preg_replace('/\?.*/', '', $url);
What Facebook does is probably a JavaScript thing, in that fashion:
Facebook 所做的可能是 JavaScript 的事情,以这种方式:
if (location.href.match(/\?.*/) && document.referrer) {
location.href = location.href.replace(/\?.*/, '');
}
回答by haz
Here's another party-trick of an answer:
这是答案的另一个派对技巧:
$url = strtok($url, '?');
$url = strtok($url, '?');
This is the answer you want if you're ever trying to win at Code Golf. It has...
如果您想在 Code Golf 中获胜,这就是您想要的答案。它有...
- the least characters
- the least lines
- the least function calls
- a sensible URL, whether or not there is a query string
- 最少的字符
- 最少的线
- 最少的函数调用
- 一个合理的 URL,无论是否有查询字符串
回答by leepowers
回答by highvolt
Without regular expressions or actually parsing the URL with parse_url, tolerant of URLs without a query string as well:
没有正则表达式或实际使用 解析 URL parse_url,也可以容忍没有查询字符串的 URL:
$url = reset((explode('?', $url)));
回答by Jan Thom?
I asssume they check the HTTP Referrer header and see if the click originated from facebook. That way they can decide wether to remove the query string or not. Something like:
我假设他们检查 HTTP Referrer 标头并查看点击是否来自 facebook。这样他们就可以决定是否删除查询字符串。就像是:
$refer=$_SERVER["HTTP_REFERER"];
if ($refer == "facebook.com") {
// this request was done by clicking a link on facebook
.. remove query string.
}
else {
// this request was done by typing the url into the browser
}
You can remove the query string by using the method netcoder suggested.
您可以使用 netcoder 建议的方法删除查询字符串。
回答by KingCrunch
Easier and more efficient, because you dont need regular expressions.
更简单、更高效,因为您不需要正则表达式。
$url = substr($url,0,strpos($url, '?'));
Another solution (if you want to retrieve the query string also)
另一种解决方案(如果您还想检索查询字符串)
list($url,$querystring) = array_pad(explode('?', $url, 2), 2, null));
回答by webbiedave
You seem to be asking two questions. How to detect if a page was visited via facebook or from an outside location and how to remove the query string from a url.
你好像在问两个问题。如何检测页面是通过 facebook 访问还是从外部位置访问,以及如何从 url 中删除查询字符串。
You can parse the referrer to see if the domain is facebook.
您可以解析推荐人以查看域是否为 facebook。
$parts = parse_url($_SERVER['HTTP_REFERER']);
if (preg_match('/(^|.)facebook.com/', $parts['host'])) {
// remove query string
}
The safest way to remove the query string is to also parse the url and then rebuild it.
删除查询字符串的最安全方法是同时解析 url,然后重建它。
$parts = parse_url('http://www.facebook.com/?ref=home');
$newUrl = $parts['scheme'].'://'.$parts['host'].$parts['path']; // http://www.facebook.com/
回答by Vijay Sharma
You can use this :
你可以使用这个:
function removeQueryStringFromURL ( $url )
{
$urlparts = parse_url($url);
if ( $urlparts != FALSE)
{
$url = http_build_url("http://[email protected]/pub/index.php?a=b#files",
array( "scheme" => $urlparts['scheme'],
"host" => $urlparts['host'],
"path" => $urlparts['path']
));
return $url;
}
return $url;
}
Remember that this code needs pecl_http extension to work.
请记住,此代码需要 pecl_http 扩展才能工作。
回答by álvaro González
A quick and dirty alternative for PHP/5.4.0 and greater:
PHP/5.4.0 及更高版本的快速而肮脏的替代方案:
$url = explode('?', $url, 2)[0];
回答by tim
Try this:
尝试这个:
$url = strtr('scheme://hostpath', parse_url($url));

