alingham Posted August 27, 2008 Share Posted August 27, 2008 Hi there... I am wanting to have an easy conditional statement that will allow scripts to perform slightly differently if its on a localhost testing server? Currently, the online server I have won't allow me to connect to the MySQL database using 'localhost' as the host. Instead it has to be hard wired in with their server name. This causes problems on the localhost server in not being able to connect to that server from outside. So I need a conditional statement that will recognise when its on localhost and when its not, so that the MySQL host can change appropriately without me having to re-type it out when I upload the files. Cheers in advance, Quote Link to comment https://forums.phpfreaks.com/topic/121498-localhost-conditional-statement/ Share on other sites More sharing options...
jordanwb Posted August 27, 2008 Share Posted August 27, 2008 I think $_SERVER['HTTP_HOST'] and $_SERVER['SERVER_NAME'] may be of some help to you. Quote Link to comment https://forums.phpfreaks.com/topic/121498-localhost-conditional-statement/#findComment-626567 Share on other sites More sharing options...
True`Logic Posted August 27, 2008 Share Posted August 27, 2008 if(stristr($_SERVER["HTTP_HOST"], "127.0.0.1")) { //admin } else { //not admin } Quote Link to comment https://forums.phpfreaks.com/topic/121498-localhost-conditional-statement/#findComment-626643 Share on other sites More sharing options...
akitchin Posted August 27, 2008 Share Posted August 27, 2008 another alternative is to just set a series of constants that define paths, server variables and whatnot, which assign differently depending on another defined constant - then just use that as your basis of switching: define('THIS_ENVIRONMENT', 0); if (THIS_ENVIRONMENT === 0) { // define local settings } else { // define server settings } it ain't pretty, but the reality is, any other contingency coding would be doing the exact same thing. it's also a good reason to have a collected settings file. Quote Link to comment https://forums.phpfreaks.com/topic/121498-localhost-conditional-statement/#findComment-626655 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.