Welcome to Dream.In.Code
Become a PHP Expert!

Join 150,053 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,734 people online right now. Registration is fast and FREE... Join Now!




Simple OR statement help

 
Reply to this topicStart new topic

Simple OR statement help

chrisd234
14 Jun, 2008 - 07:30 AM
Post #1

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 9

Hello all,

I've got a web page I'm making just for learning only. Today I decided to play with some IP address comparisons, for example making it viewable to only the two computers on my home network (192.168 range).


Eventually I might add the ability to see the sight from my work IP address.


Anyways, I've only been learning php for a few days and it's been a LONG time since I've programmed in Java or C++. Thus, I've forgotten some of the basics.


Can someone please advise me or give me some direction on why this is returning true?

CODE

<?php
include ('action.php');
  
   $ip=$_SERVER['REMOTE_ADDR'];
  
    $home = '127.0.0.1';

   if(($ip != $home)||($ip != '192.168.0.2')){
  

      //redirect_to_google();
    echo ('did it');
    }

  
?>



Basically, it pulls the IP adress of the viewer of the website and if it's not 192.168.0.2 (my other computer) or the one I'm on now it should redirect to google.

It works fine with only 1 IP address listed ie (if ($ip ==$home)) or similar. So there's some problem with how i'm setting up the OR/comparison.


Ccan someone jar my memory?


EDIT: I should clarify. The rest of the webpage is under this code. All this does is echos 'did it' on top of the webpage if for some reason my IP isnt equal to $home or .0.2.

This post has been edited by chrisd234: 14 Jun, 2008 - 07:32 AM
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: Simple OR Statement Help
14 Jun, 2008 - 07:44 AM
Post #2

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
you should have it display the IP it is getting...

and I usually don't use OR, but try this:
php
   if(($ip != $home || $ip != '192.168.0.2')){

User is offlineProfile CardPM
+Quote Post

joeyadms
RE: Simple OR Statement Help
14 Jun, 2008 - 08:19 AM
Post #3

D.I.C Head
Group Icon

Joined: 4 May, 2008
Posts: 162



Thanked: 8 times
Dream Kudos: 600
Expert In: PHP, Web Security

My Contributions
CODE

if($ip != $home || $ip !='192.168.0.2'){

}

User is offlineProfile CardPM
+Quote Post

chrisd234
RE: Simple OR Statement Help
14 Jun, 2008 - 08:47 AM
Post #4

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 9

QUOTE(joeyadms @ 14 Jun, 2008 - 09:19 AM) *

CODE

if($ip != $home || $ip !='192.168.0.2'){

}




Hmm well it's not just me, I've tried both of those and they both have the same result. If I leave out the || and just compare either of the two it works fine. When I try to add an OR it doesn't work. I may have to just write it another way.
User is offlineProfile CardPM
+Quote Post

chrisd234
RE: Simple OR Statement Help
14 Jun, 2008 - 08:54 AM
Post #5

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 9

for now I've just resorted to a nested if:

CODE



if($ip != $home){
  
    if( $ip !='192.168.0.2'){
    
    redirect_to_google();
    
    }
  }




I knew I didnt forget THAT much. I'm just glad I'm not crazy!!!

the nested if may not be pretty but it works.

This post has been edited by chrisd234: 14 Jun, 2008 - 08:55 AM
User is offlineProfile CardPM
+Quote Post

akozlik
RE: Simple OR Statement Help
14 Jun, 2008 - 09:02 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 714



Thanked: 30 times
Dream Kudos: 800
My Contributions
If you're checking that neither one of the conditions is true, you want to use AND not OR. Editing your code to read

CODE


<?php
include ('action.php');
  
   $ip=$_SERVER['REMOTE_ADDR'];
  
    $home = '127.0.0.1';

   if(($ip != $home) && ($ip != '192.168.0.2')){
  

      //redirect_to_google();
    echo ('did it');
    }
?>


should fix your problem. This is because you are checking to see if the $ip is not equal to $home AND $ip is not equal to 192.168.0.2. You say 'or', when you're saying it outloud, but you should be saying 'and. Try that and see if it works.

It works when you nest your if statements becuase it is checking that the first condition is not true, AND the second condition is not true. Basically what you're saying is if the ip is not true, move on -> if the ip is not true -> redirect. This translates into AND, not OR. It's really easy to get the two confused, and it's a problem a lot of people have, even after they've been programming for a while. I've been programming for a good amount of time now, and even I get my logic confused on occasion.

This post has been edited by akozlik: 14 Jun, 2008 - 09:04 AM
User is offlineProfile CardPM
+Quote Post

chrisd234
RE: Simple OR Statement Help
14 Jun, 2008 - 09:06 AM
Post #7

New D.I.C Head
*

Joined: 10 Jun, 2008
Posts: 9

That worked as well. Sadly, I thought of that after I posted I was going with a nested if. I should have caught that myself.


User is offlineProfile CardPM
+Quote Post

akozlik
RE: Simple OR Statement Help
14 Jun, 2008 - 09:13 AM
Post #8

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 714



Thanked: 30 times
Dream Kudos: 800
My Contributions
QUOTE(chrisd234 @ 14 Jun, 2008 - 01:06 PM) *

That worked as well. Sadly, I thought of that after I posted I was going with a nested if. I should have caught that myself.


Cool, I'm glad it helped. Like I said, it's easy to cross up your 'and's and 'or's. Or is that ands or ors. Ha ha. Either way, if you listen closely you can hear people getting them confused in grammar too as their speaking. Just remember that if one piece of logic doesn't work, try the opposite and you might be surprised!

Let us know if you have any more PHP questions. We love to help.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 10:20PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month