In This Tutorial, I am Gonna Explain you how to Redirect Back to Referrer Page after Login in PHP, At the end of the post you will find a working code available for download.
Working of this script?
What Will the Script do? The Answer of this question is in the title of the post. Take an Assumption when user tries to access a page or download a file but that access is for login users only and the user cannot access it without logging into account. 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.php – User 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’] carries the url 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>
In Dashboard Page, Write this code to check the login status of users. If User is not logged in account, it will be redirected by the script to the login page.
The Real Technique behind the working is that when user is redirected to Login Page, it will Assign the value of referral page url to a variable of Session and after login in to account, it will Redirect back to Referral page(The page from where user redirected to login page before.)
That's all for this tutorial. We Hope that this tutorial or demo code helps you to learn the functionality to redirect users back to the referring page after the process of login.
Download Code
Password For Zip File : 123456
Reviewed by Techs Awesome
on
November 18, 2020
Rating:


very useful and it helps me alot
ReplyDeletevery useful content
ReplyDelete