Page Topic

You are currently viewing Get full URL parameters in php

Get full URL parameters in php

Want to get full URL in php here is the solution –

I working one of my project which required full URL of current page. I had tried but not got result. After so many attempts i’ll found this one.

In this tutorial, you’ll learn how to get full url from your web browser address bar using PHP.

To get Current URL path We can use $_SERVER Environment variables.

Note :

We can not read URL path after Hash(#) tag in php. Because browser does not send hash data to the server. If you want to get #tag data it is possible only in javascript.

<?php 

		function url_origin( $s, $use_forwarded_host = false )
		{
		    $ssl      = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
		    $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     = ( $use_forwarded_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;
		    return $protocol . '://' . $host;
		}

		function full_url( $s, $use_forwarded_host = false )
		{
		    return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
		}

		$absolute_url = full_url( $_SERVER );

		echo "<p>".$absolute_url."</p>";

?>

For calling just echo $absolute_url;


Client (Browser) controlled variables:

Element Description
$_SERVER[‘REQUEST_URI’] The URI which was given in order to access this page; for instance, ‘/index.html‘.
$_SERVER[‘HTTP_HOST’] Contents of the Host: header from the current request, if there is one.

Server controlled variables:

Element Description
$_SERVER[‘HTTPS’] Set to a non-empty value if the script was queried through the HTTPS protocol.
$_SERVER[‘SERVER_PORT’] Returns the port on the server machine being used by the web server for communication (such as 80)
$_SERVER[‘SERVER_PROTOCOL’] Name and revision of the information protocol via which the page was requested; i.e. ‘HTTP/1.0‘;
$_SERVER[‘SERVER_NAME’] The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.

For more details go to – http://php.net

 

Leave a Reply