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
$protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
$hostname = $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_PORT'];
Why don't you get full url like this
strtolower(array_shift(explode("/",$_SERVER['SERVER_PROTOCOL'])))."://".$_SERVER['SERVER_NAME'];
or (If you want host name from HTTP)
strtolower(array_shift(explode("/",$_SERVER['SERVER_PROTOCOL'])))."://".$_SERVER['HTTP_HOST'];
Compiled from above:
function getMyUrl()
{
$protocol = (!empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) == 'on' || $_SERVER['HTTPS'] == '1')) ? 'https://' : 'http://';
$server = $_SERVER['SERVER_NAME'];
$port = $_SERVER['SERVER_PORT'] ? ':'.$_SERVER['SERVER_PORT'] : '';
return $protocol.$server.$port;
}
nothing worked serverside , something was wrong on APACHE and I had no access to the server and I ended up redirecting to http throught Javascript, It's not the ideal solution maybe this can save someone else in my situation
<script>
if(!window.location.href.startsWith('https'))
window.location.href = window.location.href.replace('http','https');
</script>
$_SERVER['SERVER_PORT']
will give you the port currently used.
<?php
$services = array('http', 'ftp', 'ssh', 'telnet', 'imap', 'smtp', 'nicname', 'gopher', 'finger', 'pop3', 'www');
foreach ($services as $service) {
$port = getservbyname($service, 'tcp');
echo $service . ":- " . $port . "<br />\n";
}
?>
This is display all port numbers.
If you already know port number you can do like this,
echo getservbyport(3306, "http"); // 80