Get full web page URL from address bar

In this tutorial, you'll learn how to get current web page url from your web browser address bar using php script.

Syntax

<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>

Overview

In this tutorial, you'll learn 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.

Code

url

In the image is a url from apple website. When you run $_SERVER['HTTP_HOST']; you'll get result "www.apple.com" only, no "http://" no "/downloads/dashboard/email_messaging/todo.html"

<?php
$server=$_SERVER['HTTP_HOST'];
echo $server;
?>
You'll get this is result
www.apple.com
When you run the script "$_SERVER['REQUEST_URI']" you'll get the result below.
no "http://" and no "www.apple.com

Sample 

<?php
$request_url=$_SERVER['REQUEST_URI'];
echo $request_url;
?>


This is a result when you run $_SERVER['REQUEST_URI']
/downloads/dashboard/email_messaging/todo.html
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;
?>

No comments:

Post a Comment