I can't get the OR operator to work.

Discussion in 'PHP' started by countrydj, Apr 18, 2011.

  1. #1
    Hi Guys..

    I'm having a bit of trouble getting the OR operator to work.
    This is the code that I have:

    global.php:
    
    $vars["admin link"] = "<a href='/path/to/admin.php'>Admin</a>";
    $vars["admin ip"] = "xxx.xxx.xxx.xx1";
    $vars["admin ip2"] = "xxx.xxx.xxx.xx2";
    
    if((getenv(REMOTE_ADDR) != $vars["admin ip"])||(getenv(REMOTE_ADDR) != $vars["admin ip2"]))
    {
    $vars["admin link"] = "";
    }
    
    Code (markup):
    index.php:
    
    require("admin/global.php"); 
    
    if(getenv(REMOTE_ADDR) == $vars["admin ip"]){
    echo $vars["admin link"];
    
    		}
    elseif
    (getenv(REMOTE_ADDR) == $vars["admin ip2"]){
    echo $vars["admin link"];
    
    		}
    
    Code (markup):
    I suspect, having played about with this code for quite a few hours, that it is:
    if((getenv(REMOTE_ADDR) != $vars["admin ip"])||(getenv(REMOTE_ADDR) != $vars["admin ip2"]))that is causing the problem.
    Can anybody advise me please.

    Regards,

    John C
     
    countrydj, Apr 18, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Based on the logic you are using in your code, the only time your OR evaluation can be false is when $vars["admin ip"] equals $vars[admin ip2"]. When those 2 variables are not equal your evaluation will always be true, because getenv(REMOTE_ADDR) will always not equal one of them. My guess is you want an AND evaluation (&&) not an OR evaluation (||).

    If that's not correct, could you post some sample data showing what your 3 variables actually are, what they evaluate to and what they should evaluate to?
     
    plog, Apr 18, 2011 IP
  3. countrydj

    countrydj Active Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #3
    Hi Plog...
    Thanks for taking the time and trouble to help me.

    This is what used to happen:

    I have an ADMIN link at the bottom of the index.php page, which is only visible if the correct ip number is accessing the page.
    This is the code that worked fine.
    global.php
    
    $vars["admin link"] = "<a href='/path/to/admin.php'>Admin</a>";
    $vars["admin ip"] = "xxx.xxx.xxx.xx1";
    
    if(getenv(REMOTE_ADDR) != $vars["admin ip"]){$vars["admin link"] = "";}
    
    Code (markup):
    index.php
    
    require("admin/global.php"); 
    
    if(getenv(REMOTE_ADDR) == $vars["admin ip"]){
    echo $vars["admin link"];
    
    Code (markup):
    Then, if a visitor came to the index.php page the ADMIN link would not show.
    If I came (my ip being xxx.xxx.xxx.xx1) to the index.php page, the ADMIN link would show at the bottom of the page.

    Now I want either of TWO ip numbers to activate the ADMIN link (maybe 3 in the future), hence the code in my first post.
    This is so that I can ADMIN the site, and so can my client.

    And now, using the code in my first post, it doesn't work for either of the ip numbers, which leads me to believe that my coding in the global.php file is wrong.


    Hope this gives a better explanation.

    Regards,

    John C
     
    countrydj, Apr 19, 2011 IP
  4. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #4
    try $_SERVER['REMOTE_ADDR'] instead of getenv

    also probably worth echoing getenv(remote_addr) to confirm is giving you the right value
     
    themullet, Apr 19, 2011 IP
  5. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #5
    Yeah, you need to change that to an AND (&&) evaluation. In your global.php file

    if((getenv(REMOTE_ADDR) != $vars["admin ip"])||(getenv(REMOTE_ADDR) != $vars["admin ip2"]))


    needs to be:

    if((getenv(REMOTE_ADDR) != $vars["admin ip"])&&(getenv(REMOTE_ADDR) != $vars["admin ip2"]))

    Suppose you came to the page (which means getenv(REMOTE_ADDR) will equal "xxx.xxx.xxx.xx1"). Walk through your current line of code using that value:

    if(("xxx.xxx.xxx.xx1" != $vars["admin ip"])||("xxx.xxx.xxx.xx1" != $vars["admin ip2"]))

    That evaluates to:

    if (FALSE || TRUE)

    Because "xxx.xxx.xxx.xx1" does not equal $vars["admin ip2"]. Since you have a TRUE evaluations in there and you are using an OR evaluation, that will evaluate to TRUE and the next line of code {$vars["admin link"] = "";} will execute.

    You need to change from an OR evaluation to an AND operation.
     
    plog, Apr 19, 2011 IP
  6. countrydj

    countrydj Active Member

    Messages:
    41
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #6
    Hi guys...

    Many thanks for taking the trouble to help me.

    I have now resolved the problem by using && instaed of ||.

    Thanks again,

    John C
     
    countrydj, Apr 19, 2011 IP
  7. ameerulislam10

    ameerulislam10 Peon

    Messages:
    461
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'm new in coding, I thought && and || suppose to do the same thing. So how come you have solved it by using && instead of ||? What is the difference between them then?

    Thanks!
     
    ameerulislam10, Apr 19, 2011 IP
  8. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #8
    && means for the entire evaluation to be true every individual evaluation in it has to be true
    || means for the entire evaluation to be true just one individual evaluation in it has to be true

    ((1>0) && (2!=7) && (3==4)) evaluates to false because at least one individual evaluation is false
    ((1>0) || (2!=7) || (3==4)) evaluates to true because at least one individual evaluation is true
     
    plog, Apr 19, 2011 IP
    ameerulislam10 likes this.
  9. ameerulislam10

    ameerulislam10 Peon

    Messages:
    461
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Oh, I think I made a mistake && means "and", and || means "or" right?
     
    ameerulislam10, Apr 19, 2011 IP