On April 9, 2011 In Coding with tags

Getting Current Page URL using PHP

This tutorial will teach you how get a URL from a webpage using php. Before starting this tutorial, we have to know about two php functions they are


Now lets get started from functions

Function : $_SERVER[‘HTTP_HOST’]

This function displays the host name of the current web page.
Syntax :
echo $host_name;

For example, we will be using the above code on a url called “http://www.mobifreaks.com/2011/04/html-introduction

Output :www.mobifreaks.com

Function : $_SERVER[‘REQUEST_URI’]

This function displays the location of files and folders of a current page.
Syntax :<?php
echo $file_name;

Output :/2011/04/html-introduction


We will assign a variable called $Get_url. For this variable we will a add string http:// and then we will add our two functions $_SERVER[‘HTTP_HOST’] and $_SERVER[‘REQUEST_URI’]. As follows
Syntax :$Get_url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

To get complete url we have added the string http:// to the two functions. when we execute the whole script we will get the output as http:// + Hostname + Filename

Finalized Code

Syntax :<?php
echo $Get_url;
Add finalized code on any .php file you want to use and get your current page url

Comments are closed.