xtian Posted July 30, 2010 Share Posted July 30, 2010 I'm working to make my code more portable when moving it between servers. I am trying to use the $_SERVER['SERVER_NAME'] global variable to change the page's URL whenever I move it. <a href="<?php print $_SERVER['SERVER_NAME'].'/index.php';?>">LINK TO SERVER'S HOME PAGE</a> This line is in a php page with this URL, http://localhost/php/tests/link_test.php However this is acting unexpectedly. The current directory of the file is being added before the server name and the link becomes, http://localhost/php/tests/localhost/index.php What am I doing wrong? Chris Quote Link to comment https://forums.phpfreaks.com/topic/209299-unexpected-results-with-_server-var-for-portable-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 30, 2010 Share Posted July 30, 2010 $_SERVER['SERVER_NAME'] is only 'localhost' Because the href= syntax that you are using is for a relative URL, the browser prepends the URL of the current page. You need to specify an absolute URL- <a href="<?php print 'http://' . $_SERVER['SERVER_NAME'] . '/index.php';?>">LINK TO SERVER'S HOME PAGE</a> Quote Link to comment https://forums.phpfreaks.com/topic/209299-unexpected-results-with-_server-var-for-portable-php/#findComment-1092882 Share on other sites More sharing options...
xtian Posted July 30, 2010 Author Share Posted July 30, 2010 Duuh. Thanks for the explanation. Do you recommend using SERVER_NAME or HTTP_HOST? Quote Link to comment https://forums.phpfreaks.com/topic/209299-unexpected-results-with-_server-var-for-portable-php/#findComment-1093253 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.