function getUrl() {
$url = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] : 'https://'.$_SERVER["SERVER_NAME"];
$url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
$url .= $_SERVER["REQUEST_URI"];
return $url;
}
Get Current Page URL
Chris Coyier
on
Thanks, really useful. I was looking for this long time. :)
Don’t suppress errors please. Especially not on an educational site like this one!
Thanks, very handy.
I think a “not equal to” as opposed to a “not identical to” should have the desired effect ($_SERVER[“SERVER_PORT”] != 80) .
Yah! It worked! I liked It… Thanks CSS-TRICKS
But there should be echo getUrl();
Why do you need to show the port number? I removed the line;
$url .= ( $_SERVER[“SERVER_PORT”] !== 80 ) ? “:”.$_SERVER[“SERVER_PORT”] : “”;
If the web server is not being addressed on a standard port, it would be necessary to include it in a URL in order to point to the correct service on the machine hosting the web server. Browsers and other HTTP clients pretty much assume port :80 as a default. SSL clients pretty much assume port :443 as a default.
That said, the code should be checking not only for port :80, but also port :443 to determine whether to omit the port number. As the code is now, it would unnecessarily add port :443 to all SSL requests.
Thanks for a useful snippet! Two comments here:
All double quotes in the snippet can safely be converted into single ones. That’s better for performance, too.
“$_SERVER[“SERVER_PORT”] !== 80″ should be replaced with “$_SERVER[“SERVER_PORT”] != 80″. The value of $_SERVER[“SERVER_PORT”] is a string so it will never strictly equal to the integer of 80. So as of now, you’re always ending up with the port number in the URL.
This is my version based on all of the above (post+comments):
Your comments are welcome…
Some improvements of the above codes:
This not working if use ssl flexible from cloudflare.
To make it work you should add:
Thanks, really useful. I was looking for this long time.