Redirect back to referral page after login

by  
Share:
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.

$_SERVER['HTTP_REFERER']

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.
<?php 
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.
<?php
if ($_SESSION["last_page_ref"] == "") {
  $_SESSION["last_page_ref"] = $_SERVER['HTTP_REFERER'];
}

if ($loginaccess == true) {
  $_SESSION["admin_name"] = $uname;
  redirect($_SESSION["last_page_ref"]);
}
?>

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.

Share:
Hasan

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

Reply
Chetan G on

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

Reply
Shahrukh Khan on

use $_SERVER[“HTTP_REFERER”]

Reply
rishikesh prasad on

Help full tutorial. thanks !

Reply
Ajaz on

Bro,
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

Reply
Hasan on

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

Reply

Leave a Reply

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