Wednesday, 3 June 2015

PHP Get Current URL

Get the current URL. Use it with social widgets that require the web page's URL 
<?php
function get_current_url($full = true)
{
 if (!$full) return $_SERVER['REQUEST_URI'];
 return 'http'.(isset($_SERVER['HTTPS']) ? 's' : '').'://'.$_SERVER['HTTP_HOST'].(($_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443) ? '' : ':'.$_SERVER['SERVER_PORT']).$_SERVER['REQUEST_URI'];
}
?>

Usage

To get the full URL, including the domain part:

echo get_current_url();

To get just the URL for internal use:

echo get_current_url(false);


This function will also work for HTTPS and non-standard ports.

0 comments:

Post a Comment