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?
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.
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.