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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
antoine
Forum Commoner
Posts: 37
Joined: Thu Jul 10, 2008 1:43 pm

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

Post 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!
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

Your placement of the NOT operator is incorrect:

Code: Select all

 
if (($_SESSION['user_name'] != "Bob") || ($_SESSION['user_name'] != "Admin")){
 
antoine
Forum Commoner
Posts: 37
Joined: Thu Jul 10, 2008 1:43 pm

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

Post 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?
antoine
Forum Commoner
Posts: 37
Joined: Thu Jul 10, 2008 1:43 pm

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

Post 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
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

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

Post 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
}
 
pkbruker
Forum Commoner
Posts: 32
Joined: Sun Aug 03, 2008 9:36 am
Location: Oslo, Norway

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

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

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

Post 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 ||.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply