Detecting which webhost I am using?

Discussion in 'PHP' started by thing2b, Nov 18, 2008.

  1. #1
    I have a custom PHP CMS that I use on a few websites hosted on different webhosts. The problem I have is that each webhost has different settings needed to connect to the database, etc. This means that for each webhost there needs to be a different config file to hold these settings.

    The problem with a config file for each webhost is that I can not just run all webhosts off the same code-base.

    What is the best way to detect which webhost the CMS is running on?

    I was sort of thinking along the lines of:

    If SERVER_NAME = a and IP = b THEN WEB_HOST = localhost
    If SERVER_NAME = c and IP = d THEN WEB_HOST = x
    If SERVER_NAME = e and IP = f THEN WEB_HOST = y
    If SERVER_NAME = g and IP = h THEN WEB_HOST = z

    I would then load the config file for WEB_HOST.

    The problem with this is that I know IP addresses will change and break it all so that is not a good idea. I am not sure how often SERVER_NAME might change.

    Any ideas?
     
    thing2b, Nov 18, 2008 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    If you have no host identification parameter, the only way I see is to upload different config files with the same name on the each host.
     
    wmtips, Nov 18, 2008 IP
  3. Shoro

    Shoro Peon

    Messages:
    143
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I suggest not using SERVER_NAME. SERVER_NAME just gives the base URL of whatever the user accessed the site with. It's possible for a user to manipulate SERVER_NAME using their hosts file. Using DOCUMENT_ROOT might be a solution is all your hosts have a docroot like /home/yourusername/public_html
     
    Shoro, Nov 18, 2008 IP
  4. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I use $host = $_SERVER["HTTP_HOST"] (returns the domain people typed into their webbrowser).
    then i strip a possible 'www.' from $host, and use $host as the name for a config file, hosted in the same subdir of my code-distribution.

    so i end up with a config file per site using my software.
    it's the granularity that's worked very well for me the last few years.

    my homedir would look as this;

    softwareDistro_1.1/php
    softwareDistro_1.1/js
    softwareDistro_1.1/config
    www/siteName/software -symlink-> /softwareDistro1.1/

    /www/siteName/.htaccess (mod_rewrite commands to point / to /software)


    as to ppl using their hosts file to fake this; all that does is switch layout and content to another site.. they'd actually get to see the site they requested.
    and since (for me at least) each site requires authentication before changes can be made, there's no hacking risk, i think.
     
    rene7705, Nov 18, 2008 IP
  5. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #5
    joebert, Nov 19, 2008 IP
  6. thing2b

    thing2b Member

    Messages:
    45
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #6
    The problem is that I play around a bit with domain names. I use my HOST file of my client system to change the IP of a domain from localhost (development) to what it should be (production/real value). In reality there is localhost-work and localhost-home, each with different db setting. In short, the domain name can not be relied upon.

    The reason I want this is that I make changes to one (like the development one) and then just push all updated files to the others. I do not want to manually change a config file on every server.
     
    thing2b, Nov 19, 2008 IP
  7. rene7705

    rene7705 Peon

    Messages:
    233
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    if i'd have 2 localhosts, i'd make sure they both use a different IP on one of their network interfaces, then connect to that ip-address instead of 'localhost'. et voila; distinction via hostname again.
     
    rene7705, Nov 19, 2008 IP