Monday, 2 February 2015

Get Full URL

Get Full URL

The PHP super global $_SERVER contains all the information you need to access various information about the URL and can even carry variables. Often you need to get the full URL of the page you are on and this requires piecing together several of the $_SERVER variables to get the whole URL.
Here we have done the work for you so you do not need to go searching through $_SERVER to find the correct super global variables to work with.

<?php
/**
 *
 * @get the full url of page
 *
 * @return string
 *
 */
 
function getAddress()
 {
    
/*** check for https ***/
    
$protocol $_SERVER['HTTPS'] == 'on' 'https' 'http';
    
/*** return the full address ***/
    
return $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
 }

 
/*** example usage ***/
 
echo getAddress();
?>

0 comments:

Post a Comment