views:

44

answers:

2
if (file_exists($cachefile) && (time() - $cachetime < filemtime($cachefile)) && $_SERVER['REQUEST_URI'] != "login" || "thankyou" || "confirm") 

Should this code work, operators wise and syntax wise?

+8  A: 

This part won't work as you (probably) want:

$_SERVER['REQUEST_URI'] != "login" || "thankyou" || "confirm"

Firstly, REQUEST_URI will never be equal to any of these values - it will have at least an additional /. If your script is named test.php, REQUEST_URI will contain at least /test.php (see below what else it might contain). Take a look at the REQUEST_URI variable to see how it is structured.

Also, REQUEST_URI is treacherous here: it will contain any additional query strings so it will be possible to confuse the comparison by adding an arbitrary ?name=value to the URL.

Do a phpinfo() to find something more fitting than REQUEST_URI, e.g. SCRIPT_NAME.

Secondly, the "or" operations you are doing will be applied to the values before the comparison is done.

You probably want something like this:

!in_array($_SERVER['SCRIPT_NAME'], 
          array("/login.php", "/thankyou.php", "/confirm.php"))
Pekka
$name = basename($_SERVER['REQUEST_URI']);$name_array = explode('.', $name);is my code for that.ah wait so it should be $name_array[0] instead. Silly me.
Sam
@Sam I suppose that's just as well. However, the `||` comparison won't work either way, the `in_array()` method shown will. Alternatively, you'd have to do a `!=` comparison on each of login, thankyou, and confirm.
Pekka
Ahh I see. Thanks for the speedy response, it worked :).
Sam
+1  A: 

Even if it does, I'd create a method for this kind of "complex" conditions.

The methods body would look something like this:

if(!condition1)
  return false;

if(!condition2)
  return false;

if(!conditionN)
  return false;

return true;

Edit: The reason is just readbility, you can name those condition methods appropriately and the methods themself are way more readable than a very long if statement.

Maxem