Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP getting full server name including port number and protocol

Tags:

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.

like image 783
ale Avatar asked Sep 15 '11 13:09

ale


People also ask

How do I find the port number of a PHP server?

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

How do I find my server port number?

How to find your port number on Windows. Type “Cmd” in the search box. Open Command Prompt. Enter the netstat -a command to see your port numbers.

Which port does PHP run on?

By default, host is set to localhost, because the built-in server is located on your machine. Use this spin box to specify the port on which the PHP built-in web server runs. By default this port is set to port 80.


1 Answers

Have a look at the documentation.

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

like image 95
Jonnix Avatar answered Oct 31 '22 18:10

Jonnix