4

我需要有关我的登录系统的帮助。在我的项目中,当用户登录时,他们只需转到他们的帐户。如果他们正在浏览某些页面并要求登录,那么他们应该在登录后重定向到该页面,而不是他们的个人资料页面。

这是我为此尝试的代码,但用户总是被重定向到 student_account.php 而不是请求的页面。

$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

5 回答 5

13

您可以将此代码粘贴到您网站上的所有页面中:

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

然后对于登录页面,您可以拥有:

<?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"); 
于 2013-05-15T07:46:20.177 回答
10

我认为您应该在此处尝试HTTP_REFERER以重定向上次访问的页面。为此在您的登录表单中设置一个隐藏字段。

HTML :-

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

redirurl并在表单帖子中获得价值。

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");

或者,如果您正在使用会话,请确保您在该页面上启动会话session_start()。否则会话将中断,并且无法保存您想要的 URL。

于 2013-05-15T07:38:42.837 回答
0

而不是代码:

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

header("Location:$url");

使用以下代码:

$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);
于 2013-05-15T07:36:03.277 回答
0

确保将“重新加载”页面信息放在第二个大括号之后。这是一个例子

<?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"); 
?>
于 2017-11-04T22:00:35.037 回答
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();
于 2017-12-27T08:02:55.880 回答