PHP Function: How To Get The Current Page URL (Address Bar Link)

Add a comment December 18th, 2009

There are number of times when we need to play with PHP and the pre-defined constants and variables. PHP has a vast number of methods which can help us in dealing with almost all issues. Today, I am gonna share with the trick to find out current page’s URL, yes the link that you see in your browser’s address bar.

Here is  the code:

<?php
    function getCurrentPageUrl(){
 
        //All URLs start with http: followed by 's' (in case of https)
        $currentPageURL = 'http';
 
        //check if the server is https based
        if ($_SERVER["HTTPS"] == 'on') {
            //attach extra 's' for https based URLs
            $currentPageURL .= 's';
        }
 
        //attach characetrs needed to form a valid URL
        $currentPageURL .= '://';
 
        //get full script URL from SERVER array
        $currentPageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 
        //return the exact current page URL
        return $currentPageURL;
    }
 
    //Usage:
    echo getCurrentPageUrl();
?>

The above code works for both http and https based URL’s. To find out more about URL’s, look at here.

Comments are welcome and if you have any tips, code related to this issue, please share it with us in comments.

Share or Bookmark this Post:
  • StumbleUpon
  • Digg
  • Twitter
  • del.icio.us
  • Facebook
  • Mixx
  • MySpace
  • Reddit
  • Google Bookmarks
  • Yahoo! Buzz
  • Technorati
  • Live
  • DZone
  • Netvibes
  • RSS
  • Print

 

  1. No comments yet.Be the first ?
  1. No trackbacks yet.
Comments feed
Real Time Web Analytics