In PHP, is there a reliable and good way of getting these things:
Protocol: i.e. http or https Servername: e.g. localhost Portnumber: e.g. 8080
Have a look at the documentation.
You want $_SERVER['SERVER_PORT']
I think.
Here's what I use:
function my_server_url()
{
$server_name = $_SERVER['SERVER_NAME'];
if (!in_array($_SERVER['SERVER_PORT'], [80, 443])) {
$port = ":$_SERVER[SERVER_PORT]";
} else {
$port = '';
}
if (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) {
$scheme = 'https';
} else {
$scheme = 'http';
}
return $scheme.'://'.$server_name.$port;
}
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,4))=='http') {
$strOut = sprintf('http://%s:%d',
$_SERVER['SERVER_ADDR'],
$_SERVER['SERVER_PORT']);
} else {
$strOut = sprintf('https://%s:%d',
$_SERVER['SERVER_ADDR'],
$_SERVER['SERVER_PORT']);
}
return $strOut;
Try something like that if you want