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
I can get the server name using $_SERVER['SERVER_NAME']
.
I ca开发者_如何转开发n kind of get the protocol but I don't think it's perfect:
if(strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,5))=='https') {
return "https";
}
else {
return "http";
}
I don't know how to get the port number though. The port numbers I am using are not 80.. they are 8080 and 8888.
Thank you.
Have a look at the documentation.
You want $_SERVER['SERVER_PORT']
I think.
The function that returns the full protocol-server-port info:
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;
}
$_SERVER['SERVER_PORT']
will give you the port currently used.
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;
}
<?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
$protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
$hostname = $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_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
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>
To get server name and port number just apply this functions
"; // Append the requested resource location to the URL echo $url .= $_SERVER['REQUEST_URI']; ?>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'];
精彩评论