Get Current Page URL

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;


  1. Damian

    Thanks, really useful. I was looking for this long time. :)

  2. Dor
    Permalink to comment#

    Don’t suppress errors please. Especially not on an educational site like this one!

  3. Adrian
    Permalink to comment#

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

  4. Nikesh Ulak
    Permalink to comment#

    Yah! It worked! I liked It… Thanks CSS-TRICKS

  5. Parker

    Why do you need to show the port number? I removed the line;
    $url .= ( $_SERVER[“SERVER_PORT”] !== 80 ) ? “:”.$_SERVER[“SERVER_PORT”] : “”;

    • Ray McCord
      Permalink to comment#

      Why do you need to show the port number?

      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.

      FUNCTION getUrl() {
        $url  = 'http' . ( ISSET( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) ? 's' : '';
        $url .= '://' . $_SERVER["SERVER_NAME"];
        $url .= ( IN_ARRAY( ARRAY( '80' , '443' ) , $_SERVER["SERVER_PORT"] ) ? ":" . $_SERVER['SERVER_PORT'] : '';
        $url .= $_SERVER['REQUEST_URI'];
        RETURN $url;
  6. Art Matsak
    Permalink to comment#

    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.

  7. Alex Parra
    Permalink to comment#

    This is my version based on all of the above (post+comments):

         * Get the current Url taking into account Https and Port
         * @link
         * @version Refactored by @AlexParraSilva
        function getCurrentUrl() {
            $url  = isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ? 'https' : 'http';
            $url .= '://' . $_SERVER['SERVER_NAME'];
            $url .= in_array( $_SERVER['SERVER_PORT'], array('80', '443') ) ? '' : ':' . $_SERVER['SERVER_PORT'];
            $url .= $_SERVER['REQUEST_URI'];
            return $url;

    Your comments are welcome…

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

We have a pretty good* newsletter.