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.
$_SERVER['SERVER_PORT'] will give you the port currently used.
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.
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.
Have a look at the documentation.
You want $_SERVER['SERVER_PORT']
I think.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With