使用PHP检测URL是否包含某个字符串,但不是参数


Detecting if a URL contains a certain string but NOT as a parameter using PHP

我正试图使用PHP基于不同的开发URL强制执行不同的调试模式。我目前有这样的设置:

$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$req_uri = $_SERVER['REQUEST_URI'];
$currentUrl = $protocol . '://' . $host . $req_uri;
$hostArray = array("localhost", "host.integration", "10.105.0"); //Don't use minification on these urls

for ($i = 0; $i < count($hostArray); $i++) {
    if (strpos($currentUrl, $hostArray[$i])) {
        $useMin = false;
    }
}

但是,使用此方法,如果要将主机数组中的任何字符串作为参数传递,则可以触发$useMin=false条件,例如:

http://domain.com?localhost

除非URL以该条件开头(或者不包含在URL参数中?之后的任何位置),否则我该如何编写能够防止$useMin=false的内容?

检查$hostArray时不要使用$currentUrl,只需检查$host本身是否在$hostArray中即可。

如果你想检查是否完全匹配:

if(in_array($host, $hostArray)) {
    $useMin = false;
}

或者,您可能想这样做,并检查$hostArray中的项目是否存在于$host:中的任何位置

foreach($hostArray AS $checkHost) {
    if(strstr($host, $checkHost)) {
        $useMin = false;
    }
}

如果您只想在$host开头的情况下在$hostArray:中查找匹配项

foreach($hostArray AS $checkHost) {
    if(strpos($host, $checkHost) === 0) {
        $useMin = false;
    }
}

我不能发表评论,所以我会在这里发布。为什么你用url检查主机阵列,为什么不直接用主机检查它,如:

if (strpos($host, $hostArray[$i])) {