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. User Avatar
    Damian

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

  2. User Avatar
    Dor
    Permalink to comment#

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

  3. User Avatar
    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. User Avatar
    Nikesh Ulak
    Permalink to comment#

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

  5. User Avatar
    Parker

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

    • User Avatar
      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. User Avatar
    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. User Avatar
    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 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…

  8. User Avatar
    Peter
    Permalink to comment#

    Some improvements of the above codes:

    private function get_act_url() {
        $act_url  = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) ? 'https' : 'http';
        $act_url .= '://' . $_SERVER['SERVER_NAME'];
        $act_url .= in_array( $_SERVER['SERVER_PORT'], array( '80', '443' ) ) ? '' : ":" . $_SERVER['SERVER_PORT'];
        $act_url .= $_SERVER['REQUEST_URI'];
        return $act_url;
    }
    

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag