Redirect to last page requested after login

I need help with my login system. In my project, when a user logs in, they just go to their account. If they look at some pages and ask to log in, they should redirect to this page after logging in, and not on the page of their profile.

Here is the code I'm trying to do for this, but the user is always redirected to student_account.php, and not to the requested page.

$fetch = mysql_fetch_assoc($exec); $_SESSION['login'] = $fetch[uniq]; $_SESSION['emailid'] = $fetch['email']; $emailid = $_SESSION['emailid']; $_SESSION['type'] = 'student'; if(isset($_SESSION['url'])) $url = $_SESSION['url']; // holds url for last page visited. else $url = "student_account.php"; // default page for header("Location:$url"); 
+4
source share
5 answers

I think you should try HTTP_REFERER here to redirect to the last page visited. To do this, set the hidden field in your login form.

HTML: -

 <input type="hidden" name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" /> 

and get the redirurl value in the form message.

PHP: -

 if(isset($_REQUEST['redirurl'])) $url = $_REQUEST['redirurl']; // holds url for last page visited. else $url = "student_account.php"; // default page for header("Location:$url"); 

Or, if you are using a session, be sure to start the session_start () session on this page. otherwise the session will break and it will not be able to save the desired URL.

+6
source

You can embed this code on all of your pages on your website:

 <?php session_start(); $_SESSION['url'] = $_SERVER['REQUEST_URI']; 

And then for the login page you can:

 <?php session_start(); // needed for sessions. if(isset($_SESSION['url'])) $url = $_SESSION['url']; // holds url for last page visited. else $url = "student_account.php"; header("Location: http://example.com/$url"); 
+9
source

Instead of code:

 if(isset($_SESSION['url'])) $url = $_SESSION['url']; // holds url for last page visited. else $url = "student_account.php"; // default page for header("Location:$url"); 

Use the following code:

 $url = ''; if(isset($_SESSION['url'])) $url = $_SESSION['url']; // holds url for last page visited. else $url = "student_account.php"; // default page for header("Location:".$url); 
0
source

Be sure to post β€œreboot” information after the 2nd curly brace. Here is an example

 <?php ob_start(); if(isset($_POST['submit'])){ $to= " email address"; $email = $_POST["email"]; $subject=$_POST["subject"]; $txt =$_POST["message"]; $email = $_POST["email"]; $headers= "From: {$email}" . "\r\n". "CC:email address"; mail($to,$subject,$txt,$headers); } header("Location: http://example.com/$url"); ?> 
0
source
 // check if session is expired if (session_status() != PHP_SESSION_ACTIVE) { // redirect to the login page header("Location: yourLogin.php?redirect=" . urlencode($_SERVER['REQUEST_URI'])); exit(); } // after successful login into yourLogin.php redirect if(!($redirect = urldecode($_GET['redirect']))) { // set default URI if don't have to redirect $redirect = 'yourDefault.php'; } header("Location: $redirect"); exit(); 
0
source

Source: https://habr.com/ru/post/1480870/


All Articles