How to Get the Current Page URL

In some cases, you might want to get the current URL of the script that is being executed. I haven’t yet found a convenient one-line function that is built-in into PHP and thus I’ve wrote the following function to do it:


/**
* Returns the current URL of the script.
* Usage: $url = cur_page_url();
*
* @access public
* @param none
* @return string
*
*/
function cur_page_url() {

$url = 'http';
if ($_SERVER["HTTPS"] == "on") {$url .= "s";}
$url .= "://";

if ($_SERVER["SERVER_PORT"] != "80") {

$url .= $_SERVER["SERVER_NAME"]. ":". $_SERVER["SERVER_PORT"]. $_SERVER["REQUEST_URI"];

} else {

$url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];

}


return $url;

}

Copy and paste the above function in your php script and when you need to get the current page, simply call it just like that:

$url = cur_page_url();

That’s all :) Pretty much simple.

Bookmark and Share

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top  banner ad


Leave a Reply