Special Offer - Get $90 Discount and Host Your Dream Web Site @ Dreamhost for 1 Year Click Here. Promo Code SLJ345
Back To Top
WordPress Web Application Development
Develop Powerfull Web Applications Using Cutting Edge WordPress Development Techniques

How to Get the Last Visited Page Url in PHP

on 2011/04/23 9:03 AM stored in: Uncategorized and tagged:

How To Get Last Visited Page Url

We can use php to get the last visited page url for a current web page. Server global variables in php provides this functionality. Any time we can get the url of the web site that requested the current page. This is known as referring site url. When you access a website directly by entering the url in the browser there will be no referring url. This is very useful to generate and identify the stats about visitors to your site. Use the code given below to track the last/previous visited url known as referring url.

 $refering_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

Practical Usage of Last Visited Page Url

1. Tracking User Stats

Insert the above code to your common header function. Since we have to record all the records this code should be kept in file common to all the files of your site.
Then create a simple table as shown below and insert data on every request.

CREATE TABLE 'refering_urls' (

Then get the results using following query and show in a page to track the sites which refers to your site.

	$output = "<table><tr><td>Url</td><td>Number of Requests</td></tr>";
	$result = "select count(id) as count,url from refering_urls group by url";
	while($row = mysql_fetch_array($result)){

	$output .= "</table>";
	echo $output;

2. Redirecting user to the previous/last visited page url after login

These days high percentage of web sites use user registration and login to allow important functionalities of the web site to user. Consider a situation where user access a specific page which requires login to proceed. If the user is currently not logged in he will be redirected to the login page. Once user logs in successfully with valid credentials, most of the web sites redirects a user to a common menu or page. User has to go through a menu or search in order to go to the page where he was trying to access before login. This is not user friendly since user wants to automatically redirect to the page he was trying to access before login. We can achieve the above functionality using referring url. Complete code for redirecting user to the previous visited page is given below.

Step 1 – Save the last accessed url in the session in every request if the user is not logged in.

 $refering_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';

 // Check whether user is already logged in
 $logged_status = isset($_SESSION['active_user']) ? '1' : '0';
 if($logged_status == '0'){
		Its important to skip the login page url before saving the last url to the session. Otherwise last url will always be the login page url.
	$login_url = "example login url";
	if($refering_url != $login_url){
		$_SESSION['last_url'] = $refering_url;

Step 2 – Redirect the user to the last visited page after login

	$last_url = $_SESSION['last_url'];
	if($last_url != ''){
		// Redirect the user to the common menu

Feel free to discuss on advantages/disadvantages or any other problem regarding this post.

Leave a Reply

Follow Us On Facebook