If $_SERVER["SERVER_NAME"]

Discussion in 'PHP' started by glasglow, Jun 19, 2007.

  1. #1
    I have a variable $A = "someword";

    I would like to use this kind of format:

    If $_SERVER["SERVER_NAME"] = 1 THEN $A = One
    If $_SERVER["SERVER_NAME"] = 2 THEN $A = Two
    If $_SERVER["SERVER_NAME"] = 3 THEN $A = Three

    Etc...

    Any idea how to do that? I've tried preg_match but I think I got it wrong trying to preg_match the server seems to be a long way around.
     
    glasglow, Jun 19, 2007 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    //you could try something like this...

    
    
    switch($_SERVER['SERVER_NAME']){
    
    case "www.yoursite.com":
    $a = "server one";
    break;
    
    case "www.mysite.com":
    $a = "server two";
    break;
    
    }
    
    //echo or do whatever you want with $a here...
    
    PHP:
     
    jestep, Jun 19, 2007 IP
  3. speda1

    speda1 Well-Known Member

    Messages:
    374
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    108
    #3
    Yes, a case switch would be better.

    If you are going to use the If Then statements, you need to use == to evaluate, not = (which assigns).
    i.e.
    If ($_SERVER['SERVER_NAME'] == 1) {
    .....
    }
     
    speda1, Jun 19, 2007 IP
  4. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you could also store the values of $A in an array, with the server domain as key:
    $A_values = array('www.server1.com' => 'server one',
      'www.server2.com' => 'server two',
      ' etc.' => 'etc.');
    
    if (array_key_exists($_SERVER['SERVER_NAME'], $A_values) {
      $A = $A_values[$_SERVER['SERVER_NAME']];
    }
    PHP:
    this saves some time if you want to add hundreds of server names :), and is easier to edit later on
     
    UnrealEd, Jun 19, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    ^^ Welcome to DP. :D

    But don't steal my posts here as well. Haha. :(
     
    nico_swd, Jun 19, 2007 IP
  6. UnrealEd

    UnrealEd Peon

    Messages:
    148
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it's gonna take a while to catch up with you :D
    Anyway, this forum is freaky: you go off for 20 minutes, and people allready posted a 100 new posts. So i guess there will be plenty of posts left for you :D
     
    UnrealEd, Jun 19, 2007 IP