27

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 can 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.

ale
  • 11,636
  • 27
  • 92
  • 149

10 Answers10

45

Have a look at the documentation.

You want $_SERVER['SERVER_PORT'] I think.

sjagr
  • 15,983
  • 5
  • 40
  • 67
Jonnix
  • 4,121
  • 1
  • 30
  • 31
  • 2
    if($_SERVER['SERVER_PORT'] != '443') { $URL = 'https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; header("location : $URL"); ?> } – Mohammed Saqib Rajput Nov 06 '15 at 07:14
13

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;
}
sbrbot
  • 6,169
  • 6
  • 43
  • 74
7

$_SERVER['SERVER_PORT'] will give you the port currently used.

Narf
  • 14,600
  • 3
  • 37
  • 66
6

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;
    }
lfjeff
  • 1,840
  • 2
  • 17
  • 19
3
<?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
Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35
2
$protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
$hostname = $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_PORT'];
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
1
 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

Kristian
  • 21,204
  • 19
  • 101
  • 176
DarkMantis
  • 1,510
  • 5
  • 19
  • 40
0

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>
Jonathan DS
  • 2,050
  • 5
  • 25
  • 48
0

To get server name and port number just apply this functions

"; // Append the requested resource location to the URL echo $url .= $_SERVER['REQUEST_URI']; ?>
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 22 '22 at 20:46
-2

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'];
Ankur
  • 5,086
  • 19
  • 37
  • 62
Avesh
  • 1
  • 1
    See [this](http://stackoverflow.com/questions/30392047/array-shift-only-variables-should-be-passed-by-reference-error-in-php), if u want to use `array_shift()`, it's about strict standards. – Boolean_Type Mar 18 '16 at 09:21