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
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)
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>
Comments
Post a Comment