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 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> 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? 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
Archived
This topic is now archived and is closed to further replies.