4

I need help regarding my login system. In my project when a user logs in they simply go to their account. If they are browsing some pages and asked to login then they should redirect to that page after login and not their profile page.

Here is the code I am trying for this but the user is always redirected to student_account.php and not 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"); 
Belinda
  • 1,230
  • 2
  • 14
  • 25
Dinesh
  • 447
  • 1
  • 6
  • 22

5 Answers5

13

You can paste this code into all your pages on your website:

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

And then for the login page you can have:

<?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"); 
Yogus
  • 2,307
  • 5
  • 20
  • 38
10

I think you should try HTTP_REFERER here to redirect on last visited page. for that set a hidden field in your login form.

HTML :-

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

and get redirurl value in form post.

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 session then please ensure that you start session session_start() on that page. otherwise session will break and it couldn't save your desired URL.

Roopendra
  • 7,674
  • 16
  • 65
  • 92
  • 1
    This will always redirect to login form page, right? And just an information from the documentation : "This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted." – Yann39 Dec 14 '14 at 15:03
0

Instead of the 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

Make Sure you place the 'reload' page information after the 2nd curly Bracket. 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"); 
?>
crackmigg
  • 5,571
  • 2
  • 30
  • 40
Shams
  • 1
0
// 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();