SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trying to understand || for if statement

    How would I format the following code to check for multiple $userip results?

    Such as IP's:
    173.33.202.122, 68.146.120.46


    Code:
    $userip = ($_SERVER['X_FORWARDED_FOR']) ? $_SERVER['X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
    
    
    	 if ($userip == "173.33.202.122"){ 
     
    	 echo "You are blocked from using this.";
                  include "footer.php";
                  die;
    	 }

    I tried this below, but something is wrong here....other than me being new to PHP

    if ($userip == "173.33.202.122" || "68.146.120.46"){

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    if ($userip == "173.33.202.122" || $userip == "68.146.120.46"){ 
    An easier way to check against multiple IP's is to use an array

    PHP Code:
    $check_ip = array('173.33.202.122','68.146.120.46');

    if (
    in_array($userip$check_ip)) {
        
    // PHP code to run here


  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Id use a switch...

    PHP Code:
    switch($userip){

          case 
    '173.33.202.122' /*do stuff*/; break;
          case 
    '68.146.120.46' /*do stuff*/; break;


  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by david.j View Post
    Id use a switch...
    The only downside to a switch is if at a point you decide to change to a database driven IP blocker you would just append them as a case within it so an array would be the best option.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    I would personally go with an array too, not to say that a switch doesnt have it's uses though.
    To answer the OP's original question thought, you have to compare each value as you can 'string together' when comparing variables

    PHP Code:
    if ($userip == "173.33.202.122" || $userip == "68.146.120.46"){ 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •