To Get Full URL in PHP:
In this post, We are discussing about How to get the full URL in PHP .
SOLUTION 1:
Have a look at:
php code
$_SERVER['REQUEST_URI']
[ad type=”banner”]
javascript code
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Note : That the double quoted string syntax is perfectly correct
If we want to support both HTTP and HTTPS , we can use
php code
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
Using this code has security implications . The client can set HTTP_HOST and REQUEST_URI to any arbitrary value
SOLUTION 2:
Using a ternary statement:
php code
$url = "http" . (($_SERVER['SERVER_PORT'] == 443) ? "s://" : "://") . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
[ad type=”banner”]
SOLUTION 3:
Cross platform method for finding the current URL is:
php code
$url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
SOLUTION 4:
Sample code:
javascript code
function full_path() $s = &$_SERVER; $ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false; $sp = strtolower($s['SERVER_PROTOCOL']); $protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : ''); $port = $s['SERVER_PORT']; $port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port; $host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null); $host = isset($host) ? $host : $s['SERVER_NAME'] . $port; $uri = $protocol . '://' . $host . $s['REQUEST_URI']; $segments = explode('?', $uri, 2); $url = $segments[0]; return $url; }
SOLUTION 5:
javascript code
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
It works for all webservers (Apache , nginx, IIS, …):
SOLUTION 6:
we can get full current URL including $_SERVER[‘REQUEST_URI’].
Function:
php code
function getCurrentUrl($full = true) { if (isset($_SERVER['REQUEST_URI { $parse = parse_url( (isset($_SERVER['HTTPS']) && strcasecmp($_SERVER['HTTPS'], 'off') ? 'https://' : 'http://') . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '')) . (($full) ? $_SERVER['REQUEST_URI'] : null) ); $parse['port'] = $_SERVER["SERVER_PORT"]; // Setup protocol for sure (80 is default) return http_build_url('', $parse); } }
[ad type=”banner”]
Test code:
javascript code
// Follow $_SERVER variables was set only for test $_SERVER['HTTPS'] = 'off'; // on $_SERVER['SERVER_PORT'] = '9999'; // Setup $_SERVER['HTTP_HOST'] = 'some.crazy.server.5.name:8088'; // Port is optional there $_SERVER['REQUEST_URI'] = '/150/tail/single/normal?get=param'; echo getCurrentUrl(); // http://some.crazy.server.5.name:9999/150/tail/single/normal?get=param echo getCurrentUrl(false); // http://some.crazy.server.5.name:9999/
SOLUTION 7:
Function to handle the URL:
php code
<?php function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s"; } $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } ?>
SOLUTION 8:
javascript code
$current_url = sprint ( '%s://%s/%s', isset($_SERVER['HTTPS']) ? 'https' : 'http', $_SERVER['HTTP_HOST'], $_SERVER['REQUEST_URI'] );
SOLUTION 9:
Code:
javascript code
$base = "http://$_SERVER[SERVER_NAME]:$_SERVER[SERVER_PORT]$my_web_base_path"; $url = $base . "/" . dirname(dirname(__FILE__));
SOLUTION 10:
Here is the another way of solution:
javascript code
//Fetch page URL by this $url = $_SERVER['REQUEST_URI']; echo "$url<br />"; //It will print //fetch host by this $host=$_SERVER['HTTP_HOST']; echo "$host<br />"; //You can fetch the full URL by this $fullurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $fullurl;
[ad type=”banner”]
SOLUTION 11:
php code
print_r($_SERVER);
$_SERVER is an array containing information such as headers, paths, and script locations.
The entries in this array are created by the web server .
$HTTP_SERVER_VARS contains the same initial information, but is not a superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER are different variables.)