The following code is used to get full url address by using php.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
Notes:
Here we are using 2 functions in php to get full url from address bar.
1. $_SERVER['HTTP_HOST']
2. $_SERVER['REQUEST_URI']
$_SERVER['HTTP_HOST'] - This function will show only server name.
$_SERVER['REQUEST_URI'] - This function will show you the path to file of your url.
When you run $_SERVER['HTTP_HOST']; for this website you'll get result "www.techniqzone.blogspot.in" only, no "http://" no "/Get URL from address bar - PHP"
<?php
$server=$_SERVER['HTTP_HOST'];
echo $server;
?>
Output is "www.techniqzone.blogspot.in".
This script "$_SERVER['REQUEST_URI']" display the result like below.
no "http://" and no "www.techniqzone.blogspot.in
Example:
<?php
$request_url=$_SERVER['REQUEST_URI'];
echo $request_url;
?>
Output of $_SERVER['REQUEST_URI'] is "/Get URL from address bar - PHP"
If you want to get full URL from this site you, we have to use (.) to connect 2 functions together and create http:// by yourself.
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
0 comments:
Post a Comment