Grow your CSS skills. Land your dream job.

Last updated on:

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;
}

Comments

  1. 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. 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"] : “”;

    • 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. 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 http://css-tricks.com/snippets/php/get-current-page-url/
         * @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

Current day month ye@r *

*May or may not contain any actual "CSS" or "Tricks".