Page 1 of 1

NOT operator (!) doesn't seem to be working...

Posted: Fri Jul 25, 2008 2:18 pm
by antoine
Basically, the NOT (!) operator in my PHP code does not seem to be working...

This is a continuation from my other post "Why isn't this code working", but it seems that I have narrowed down the problem to the not operator.

Rather than reiterate it all here, please just go to that topic and check it out. It really should work and I can't figure out why it doesn't.

Right now I temporarily have the code:

Code: Select all

<?php
if ( $_SESSION['verified'] == "" ) {
   header ("Location:http://www.mydomain.com/no_access.html");
   // I have also used exit; here, but that didn't help either
}
?>
This works, but I shouldn't have to use it and it won't allow me to only let a specific user and an admin view it, because for that to be efficient, I'd need:

Code: Select all

<?php
if ((!$_SESSION['user_name'] == "Bob") || (!$_SESSION['user_name'] == "Admin")){
    header ("Location:$_SESSION[redirect]"); // A predefined complete URL, client-specific
}
?>
Otherwise, I'd have to list all other users (not gonna happen!).

So if you could read that topic and see any possible reasons why it doesn't work, or at the least an equally convenient way of achieving the second piece of code without using a !, put 'er 'ere!

Re: NOT operator (!) doesn't seem to be working...

Posted: Fri Jul 25, 2008 2:29 pm
by Eran
Your placement of the NOT operator is incorrect:

Code: Select all

 
if (($_SESSION['user_name'] != "Bob") || ($_SESSION['user_name'] != "Admin")){
 

Re: NOT operator (!) doesn't seem to be working...

Posted: Fri Jul 25, 2008 2:33 pm
by antoine
Thanks pytrin, but I have already tried that and it is not working either. As I tested though (just to make sure), the page loads without any redirect, and 'verified' is blank (I also tested that). Nobody is even signed in!

I do have session_start() as well...

This is strange. Any other ideas?

Re: NOT operator (!) doesn't seem to be working...

Posted: Fri Jul 25, 2008 5:51 pm
by antoine
UPDATE:

I solved the problem by changing the 'or' to an 'and', like this:

Code: Select all

<?php
 
session_start();
 
if ( ($_SESSION[first_name] != "Bob") && ($_SESSION[first_name] != "Admin") ){
    header ("Location:$_SESSION[redirect]");
    exit;
}
 
?>
I guess this made sense. I really thought about it and what was happening was, if the users first name was EITHER "Not Bob" OR "Not Admin", it would return true. That way, if Admin was signed in, it was "Not Bob", so it redirected. By changing it to and, it made it check that if Bob or Admin was signed in, it would return false.

So, thanks for everyone's help, and I hope this may help someone else out later on.

Also, I was surprised no one caught that before me!

Cheers,
Antoine

Re: NOT operator (!) doesn't seem to be working...

Posted: Fri Aug 08, 2008 6:56 am
by pkbruker
Actually, the not operator needs some parenthesises:

Code: Select all

 
<?php
 if ((!($_SESSION['user_name'] == "Bob")) || (!$_SESSION['user_name'] == "Admin")){
    header ("Location:$_SESSION[redirect]"); // A predefined complete URL, client-specific
}
 

Re: NOT operator (!) doesn't seem to be working...

Posted: Fri Aug 08, 2008 7:14 am
by pkbruker
I've been looking a bit further into this, to find a logical explanation, and I think it's like this:

Example code:

Code: Select all

 
$name="John";
 
if(!$name=="Jenny") print "Name is not John"; // Will output nothing
if(!($name=="Jenny")) print "Name is not John"; // Will output Name is not John
 
The first statement will compate !$name to "Jenny", which does not make sense (what is !$name??), whereas the second will evaluate ! of the logical expression ($name=="Jenny"). That's why you need the peranthesises.

Re: NOT operator (!) doesn't seem to be working...

Posted: Fri Aug 08, 2008 9:43 am
by pickle
It makes perfect sense. !$name means the inverse of $name, which will usually evaluate to FALSE if $name is populated. !($name=="Jenny") means the inverse of the evaluation of the statement $name=="Jenny".

You've re-discovered a common rule in Boolean logic (it has a name, but I forget): The inverse of an && statement is found by !-ing both operands & changing the operator to an ||.