24

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
  • 10,980
  • 25
  • 89
  • 145

9 Answers9

45

Have a look at the documentation.

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

sjagr
  • 15,254
  • 4
  • 38
  • 65
Jonnix
  • 4,101
  • 1
  • 29
  • 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
8

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

Narf
  • 14,381
  • 3
  • 36
  • 65
6

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
  • 5,567
  • 6
  • 38
  • 68
5

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,640
  • 1
  • 16
  • 18
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
  • 5,998
  • 2
  • 34
  • 35
2
$protocol = isset($_SERVER['HTTPS']) && (strcasecmp('off', $_SERVER['HTTPS']) !== 0);
$hostname = $_SERVER['SERVER_ADDR'];
$port = $_SERVER['SERVER_PORT'];
Alix Axel
  • 147,060
  • 89
  • 388
  • 491
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
  • 20,416
  • 18
  • 93
  • 168
DarkMantis
  • 1,500
  • 4
  • 19
  • 39
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
  • 1,934
  • 3
  • 24
  • 45
-1

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,048
  • 19
  • 36
  • 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