All PHP Tutorials
Ad Management
Counter
Email System
Forum and Guestbook
File Upload
Image Manipulation
Login / Members / Password
Pagination
Encode Script
Refresh / Redirection
Miscellaneous
All MySQL Tutorials
Create, Manage Database using phpMyAdmin
Connect to Database
Insert Data
Select Data
Edit Database
Update Database
Delete Database
Order Results



Home > PHP Tutorials
Get full web page URL from address bar
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.


Example
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 this script "$_SERVER['REQUEST_URI']" you'll get the result below, no "http://" and no "www.apple.com

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

This is 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;
?>


Display URL in text box (included javascript, click to select all text in text box)
youtube url


This is an example of javascript and php script, click to select all text in text box like youtube.com or many other site.

############### Code

<html>
<head>
<title>Get url for address bar</title>

<script type="text/javascript">
function select_all()
{
var text_val=eval("document.form_name.type");
text_val.focus();
text_val.select();
}
</script>

</head>
<body>

<form name=form_name method=post action=''''>
<input name="type" type="text" id="type" value="
<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?>" size="50" onClick="select_all();">
</form>

</body>
</html>



Random
 
PHP Redirection script
Learn how to redirect in this tutorial, you can use header(); function in php or use meta to redirect to a new page you want or redirect to other website. 
 
 
   
Hosted by Hostgator
© PHPeasystep.com 2005-2008