Suppose you have two or more domains that have image-copies of the same websites, and somewhere you would need to give a URL based on the current domain. For example, you would need to get values like http://helloacm.com or https://ranplan.co.uk or https://codingforspeed.com:8080 etc.
In PHP, the $_SERVER array contains some values that are useful to accomplish this task. The $_SERVER[‘SERVER_NAME’], $_SERVER[‘HTTP_HOST’] and $_SERVER[‘SERVER_PORT’].
1 2 3 4 5 6 7 8 9 10 11 | $server_port = $_SERVER['SERVER_PORT']; if ($server_port == 443) { $server_name = "https://".$_SERVER['SERVER_NAME']; } else if ($server_port != 80) { $server_name = "http://".$_SERVER['SERVER_NAME']. ':' . $_SERVER['SERVER_PORT']; } else { $server_name = "http://".$_SERVER['SERVER_NAME']; } |
$server_port = $_SERVER['SERVER_PORT']; if ($server_port == 443) { $server_name = "https://".$_SERVER['SERVER_NAME']; } else if ($server_port != 80) { $server_name = "http://".$_SERVER['SERVER_NAME']. ':' . $_SERVER['SERVER_PORT']; } else { $server_name = "http://".$_SERVER['SERVER_NAME']; }
The difference between $_SERVER[‘SERVER_NAME’] and $_SERVER[‘HTTP_HOST’] is that the HTTP_HOST returns the port number while SERVER_NAME does not. The above script ignores adding port 80 and forces https if port 443 is used.
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: How to Create UUID in PHP?
Next Post: How to Back Up Crontab List Automatically on Linux Servers?