treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Get Current Page URL

Last updated on:

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

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

Leave a Comment

Use markdown or basic HTML and be nice.