1

I'm trying to force different modes of debugging based on different development urls using PHP. I currently have this set up:

$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;
    }
}

However, using this method, you would be able to trigger the $useMin = false condition if you were to pass any of the strings in the host array as a parameter, for example:

http://domain.com?localhost

How do I write something that will prevent $useMin = false unless the URL starts with that condition (or is not contained anywhere after the ? in the url parameter)?

mheavers
  • 29,530
  • 58
  • 194
  • 315

2 Answers2

2

Don't use the $currentUrl when checking $hostArray, just check to see if the $host itself is in the $hostArray.

If you want to check for an exact match:

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

Or maybe you want to do this, and check to see if an item in $hostArray exists anywhere within your $host:

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

And if you only want to find a match if $host starts with an item in $hostArray:

foreach($hostArray AS $checkHost) {
    if(strpos($host, $checkHost) === 0) {
        $useMin = false;
    }
}
jszobody
  • 28,495
  • 6
  • 61
  • 72
1

I can't comment so I'll post here. Why do you check the host array with the url, why not check it directly with the host as in:

if (strpos($host, $hostArray[$i])) {
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
adrian.budau
  • 349
  • 1
  • 6
  • hey - this doesn't seem to work for some reason. Maybe I'm not understanding how this function works, but if I use your code above, and access the site at http://localhost - despite the fact that both $host and $hostArray[$i] echo as "localhost", that if conditional never triggers – mheavers Aug 16 '13 at 20:54
  • Yeah, sorry. Use: if (strpos($host, $hostArray[$i]) !== false) { The problem was when the match was at the begining. – adrian.budau Aug 16 '13 at 21:58