Redirect back to referral page after login

Redirect back to referral page after login

Have you ever notice in some websites when you want to open a webpage directly or indirectly that has some restricted access and then you are redirected to a login page, after you login through that you are redirected back to the same page and not to their home page(primary page after login). In this post I will be explaining how to redirect back to referral page after login with PHP.

View Demo

Scope of this script?

What will this script do? The title of this post answers the question. Take a scenario where someone tries to access a webpage or download a picture but that access is for priviliged users only and the user cannot access it without prior login. So user will have to login first and then redirect it back to the referral page.

Page structure of the script

Have a look at the pages that you will be using to run this script.

  • index.php – The main page or landing page. From this page user will try to access a dashboard page which is for admin users only.
  • dashboard.php – This page can viewed after login only.
  • login.phpUser who are not logged in can login from here, Logged in user will be redirected to dashboard page.
  • logout.php – Logs out user and redirect them back to site index.php page.

It’s time for you to see the hero of this script that does the trick. $_SERVER[‘HTTP_REFERER’].

$_SERVER[‘HTTP_REFERER’] contains the address of the page that has referred to the current page. But if you reload the current page twice the value is set to null. It will work only for the first time when the page is redirected from different page.


When you visit the site, index.php page will be the home page. you will then try to access dashboard.php without login.
<a href="dashboard.php" >Access Admin Dashboard</a>

On dashboard page, place a security check to check the login status for users. So if the user is not logged in they will be redirected to login page.
if (!$_SESSION["admin_name"]) redirect("login.php");

The login page is just by the book nothing fancy. The only trick is that whenever user is redirected to login page it will assign the referral page URL to a session variable and after successful login it will redirect back to its referral page.
if ($_SESSION["last_page_ref"] == "") {
  $_SESSION["last_page_ref"] = $_SERVER['HTTP_REFERER'];

if ($loginaccess == true) {
  $_SESSION["admin_name"] = $uname;

This is the end of this tutorial. Hope this helps you yo learn the technique of redirecting users back to referral page after login.

View Demo

Share to download the Source Codes for FREE!
We're glad to give free downloads, but we need your love to carry on making that.
Please support us by sharing the page.

Get the latest updates directly in your inbox for FREE: Subscribe here

Share your feedback / let me know your doubts regarding this tutorial in the comment box given below.


Shahrukh Khan (Hasan)

A software engineer who's a die-hard coder, blogger, dreamer and mentor with years of expertise in web development. Know more...

Related Posts

ankit on

thanks for this

Chetan G on

I need to redirect to the previous page after login.
How should i get the previous page??

Shahrukh Khan on


rishikesh prasad on

Help full tutorial. thanks !

Ajaz on

If you don’t mind I want to use this on my webpage. Content will be visible for everyone but download will be only for users. When user clicks on file download link, it should redirect to Login page and after successful logon it should come back to same page. If you can guide me code, that would be great, Thanks

Hasan on

I guess the script is already given use the same logic.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.