The Letter E Posted January 4, 2013 Share Posted January 4, 2013 Here's my way, anyone have a cleaner way? Or one that uses less bytes?? $server = ((preg_match('/http/i', $_SERVER['SERVER_PROTOCOL'])) ? 'http' : 'https').'://'.$_SERVER['SERVER_NAME'].(($_SERVER['SERVER_PORT'] != 80) ? ':'.$_SERVER['SERVER_PORT'] : ''); Thanks, E Quote Link to comment https://forums.phpfreaks.com/topic/272702-is-there-a-better-way-to-get-the-root-server-address/ Share on other sites More sharing options...
kicken Posted January 4, 2013 Share Posted January 4, 2013 If you're looking to generate the base URL, eg http://example.com/ or https://example.com, something like this should be fine: $baseurl = sprintf("%s://%s/", !empty($_SERVER['HTTPS'])?'https':'http', $_SERVER['HTTP_HOST']); That will use the same protocol and hostname/port that the page was requested with to generate the base url. Quote Link to comment https://forums.phpfreaks.com/topic/272702-is-there-a-better-way-to-get-the-root-server-address/#findComment-1403264 Share on other sites More sharing options...
The Letter E Posted January 4, 2013 Author Share Posted January 4, 2013 Nice one! Out of curiosity, is there a difference in overhead doing !empty() vs empty()? Quote Link to comment https://forums.phpfreaks.com/topic/272702-is-there-a-better-way-to-get-the-root-server-address/#findComment-1403271 Share on other sites More sharing options...
kicken Posted January 4, 2013 Share Posted January 4, 2013 Nothing worth worrying about. Quote Link to comment https://forums.phpfreaks.com/topic/272702-is-there-a-better-way-to-get-the-root-server-address/#findComment-1403282 Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 Nice one! Out of curiosity, is there a difference in overhead doing !empty() vs empty()? No. Please don't worry about optimizing microseconds out of PHP code. It's a worthless endeavor. Quote Link to comment https://forums.phpfreaks.com/topic/272702-is-there-a-better-way-to-get-the-root-server-address/#findComment-1403289 Share on other sites More sharing options...
The Letter E Posted January 4, 2013 Author Share Posted January 4, 2013 I figured as much. Quote Link to comment https://forums.phpfreaks.com/topic/272702-is-there-a-better-way-to-get-the-root-server-address/#findComment-1403297 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.