Jump to content

Is there a better way to get the root server address?


The Letter E

Recommended Posts

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

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.