SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2010
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_SERVER['SERVER_NAME']; <-- any caveats?

    Lately I have been writing function after function that gets my server name
    www.example.com from an .ini file which I created... then I find this: $_SERVER['SERVER_NAME']. Is there anything wrong with using $_SERVER['SERVER_NAME'] in production? Any caveats, any pitfalls, any dangers?

    Thanks in advance,

  2. #2
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just setup a constant in your php --
    define('SITE_URL', 'www.example.com');

    I've seen some debate on $_SERVER['SERVER_NAME'] and $_SERVER['HTTP_HOST'], which require Apache to have your VHost canonical name setup, otherwise you'll get the wrong data. But I know very little about it, I've used the $_SERVER variable once or twice, not much, and can't recall having any problems with it.

  3. #3
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cron jobs often execute a PHP script directly (i.e. without the webserver being used) so the $_SERVER variable won't exist.

  4. #4
    SitePoint Evangelist hessodreamy's Avatar
    Join Date
    Apr 2005
    Location
    uk
    Posts
    524
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Also worth noting that SERVER_NAME will return your domain name, if on a live server, or localhost, if that's what's in your address bar. Or, if you call the site via an ip address eg 127.0.0.1 instead of localhost, then it'll return the ip address. You might have already considered this, but it catches some people out.


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
  •