检查 url 在 PHP 中包含 String 和 QueryString 的最佳方法
Best way to check url contain a String and QueryString in PHP
我正在尝试检测 url 是否包含 string 或带有 querystring 的 string,然后给它一个不同的功能。
下面的代码是我能想到的,但它不起作用。
最好的检查方法是什么?
url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page
if (stripos($_SERVER['REQUEST_URI'], 'index')){
$xurl="http://example2.com/photos/";
} else {
$req_url = $_SERVER['QUERY_STRING'];
$xurl=('http://example2.com/photos/' . $req_url . '');
}
您应该检查“?”在请求而不是索引中。
url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page
if (stripos($_SERVER['REQUEST_URI'], '?') === false ){
$xurl="http://example2.com/photos/";
}
else{
$req_url = $_SERVER['QUERY_STRING'];
$xurl=('http://example2.com/photos/' . $req_url . '');
}
如果有查询字符串,它将在需要时填充 the $_GET
superglobal. Just check for that and add it back using http_build_query()
(这将确保处理任何转义。)
$xurl = "http://example2.com/photos/";
if (!empty($_GET)) {
$xurl .= "?" . http_build_query($_GET);
}
用 parse_url 将 URL 分成几个部分,然后仔细检查您需要什么...
http://php.net/manual/de/function.parse-url.php
我正在尝试检测 url 是否包含 string 或带有 querystring 的 string,然后给它一个不同的功能。
下面的代码是我能想到的,但它不起作用。
最好的检查方法是什么?
url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page
if (stripos($_SERVER['REQUEST_URI'], 'index')){
$xurl="http://example2.com/photos/";
} else {
$req_url = $_SERVER['QUERY_STRING'];
$xurl=('http://example2.com/photos/' . $req_url . '');
}
您应该检查“?”在请求而不是索引中。
url 1 = http://example.com/index.php
url 2 = http://example.com/index.php?some-test-page
if (stripos($_SERVER['REQUEST_URI'], '?') === false ){
$xurl="http://example2.com/photos/";
}
else{
$req_url = $_SERVER['QUERY_STRING'];
$xurl=('http://example2.com/photos/' . $req_url . '');
}
如果有查询字符串,它将在需要时填充 the $_GET
superglobal. Just check for that and add it back using http_build_query()
(这将确保处理任何转义。)
$xurl = "http://example2.com/photos/";
if (!empty($_GET)) {
$xurl .= "?" . http_build_query($_GET);
}
用 parse_url 将 URL 分成几个部分,然后仔细检查您需要什么... http://php.net/manual/de/function.parse-url.php