登录后将用户重定向到他们已退出的页面 [英] Redirect User to the page they've left off once they had log in

查看:102
本文介绍了登录后将用户重定向到他们已退出的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:实现了新代码。它可以正常工作,但不能处理最后带有?id = 的页面。

Implemented new code. It work but it doesn't tackle the page that has ?id= at the end.

还有其他方法可以解决此类问题吗?

Is there any other way to solve this kind of problem?

给出代码段以检测用户是否

Given the snippet to detect if the user is logged in at every page is this:

<?php
    session_start();
    error_reporting(0);
    include('includes/config.php');
    include('includes/config1.php');

    if(strlen($_SESSION['emplogin'])==0){
        $_SESSION['last_page'] = $_SERVER['PHP_SELF'];
        header('location:../login.php');
    } 
?>

鉴于login.php代码是这样的:

Given the login.php code is this:

<?php
session_start();
error_reporting(0);
include('includes/config.php');

if(isset($_POST['signin']))
{
    //sign in code

if($status==0)
{
    $msg="Your account is Inactive. Please contact admin";

} else{
    if(isset($_SESSION['last_page'])) {
        $last_page = $_SESSION['last_page'];
        header("Location: $last_page");
// And remember to clean up the session variable after
// this is done. Don't want it lingering.
        unset($_SESSION['last_page']);
    }else{echo "<script type='text/javascript'> document.location = 'login.php'; </script>";}

} 
}

else{
  echo "<script>alert('Invalid Details');</script>";
}

}
?>


推荐答案

使用 header() 重定向成功更新,条件是if / elststmt。

Use a header() redirect in your successful update conditional if/else stmt.

if($query->rowCount() > 0)
{
    foreach ($results as $result) {
        $status = $result->Status;
        $_SESSION['eid'] = $result->id;
        $_SESSION['name'] = $result->FirstName . " " . $result->LastName;
        $_SESSION['emplogin'] = $result->emp_username;
    }

    if($status == 0)
    {
        $target_page = 'myprofile.php'; // I assume this is the page you are redirecting to 
                                    // on success, change this to your desired link if not.

        //Build your entire http path URL. 
        $url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
        $url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
        $url .= $target_page.'?success';  // <-- Your relative path with a success post through url            
        header('Location: ' . $url, true, 302);
        exit;
    } else {
        echo "<script type='text/javascript'> document.location = 'myprofile.php'; </script>";
    }

} else {
    //else $query->rowCount() !> 0 ***no results...*** 
    $target_page = 'myprofile.php'; 
    $url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
    $url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
    $url .= $target_page.'?log_error';  // <-- Your relative path with an error post through url, handle $_GET['log_error'] on another page or this page and redirect.          
    header('Location: ' . $url, true, 302);
    exit;
}

别忘了添加 if(isset ($ _GET ['success'])){$ success =您的成功消息在这里} 在您的目标页面上,以及 if(isset($ _ GET ['log_error'] )){$ log_error =您的登录错误消息在这里} 。然后在要发布成功/错误消息的位置发布该变量。

Don't forget to add an if(isset($_GET['success'])){ $success = "Your success message here" } on your target page and if(isset($_GET['log_error'])){ $log_error = "Your login error message here" }. Then post that variable where you wish to post your success/error message/'s.

您可以使用相同的重定向并将不同的POST键/值对添加到URL并筛选POST结果。因此,您可以输入?error = login 而不是?成功,然后使用条件检查来处理该错误。如果设置了$ _GET ['error']且=设置为'login' if(isset($ _ GET ['login')&& $ _GET ['login')===错误){//在此处处理错误代码并显示问题}

You can use the same redirect and add different POST key/value pairs to the URL and sift through the POST result. So instead of ?success, you could put something like ?error=login then handle that error with a conditional that checks if the $_GET['error'] is set and = to 'login' if(isset($_GET['login') && $_GET['login' ) === "error"){ //handle error code here and display issue }.

会议 >
创建一个会话并在其中存储相关信息,例如 userLoggedIn,该信息将在成功登录的用户登录页面上设置。

SESSIONS Create a session and store relevant info there like 'userLoggedIn' which would be set at the user login pages successful log in.

 session_start();// Start the session
 // $_SESSION['sessData'] is an array that carries pertinent SESSION info that can be 
 // logged through $_SESSIONS within your user login pages
 $sessData = !empty($_SESSION['sessData'])?$_SESSION['sessData']:'';
 // check to see if the user is logged in, if so send them to the restricted 
 // home page for logged in users
 if( isset($_SESSION['userLoggedIn'])!="" ){
    header("Location: home.php"); // home.php is the users logged in page. 
 }
 //handle code if session is not set

2020年3月19日编辑:

如果您有一个保存用户数据的数据库,请为他们注销时所在的页面创建一个表,将其称为 logout_page 或其他东西

If you have a DB that saves user data create a table for the page they are on when they logout, call it logout_page or something

在您的html中,请确保每个页面的body标签中都设置了唯一的ID,以便您可以调用关于设置过去访问页面的变量,该变量将在注销时发送到数据库。

In your html make sure each page has a unique ID set in the body tag so you can call on that when setting past page visited variable that will be sent to DB when they log out. Set this in php and call in your html.

// Declare a variable in your php on each restricted login page the user can access and set it to the following.
// You can use `basename()` and `$_SERVER['PHP_SELF']` to get current page file name.

$pageName = basename($_SERVER['PHP_SELF']);

// conditional to see if user is logging out

if(isset($_GET['logout'])){// $_GET value coming from your logout button that directs to this code        
    //query DB and ad $pageName to your DB entry
    //handle logout 
}

当用户登录时,请更改登录脚本并将 last_page 包括在结果查询中。

When the user logs in alter the login script and include the last_page to your results query.

// not sure how you connect but it would look similar to this
$sql = "SELECT id, first_name, last_name, email, last_page FROM user_table";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
         //assign values to variables
         $id = $row['id'];
         $target_page = $row['logout_page'];
         // Set sessions here
         $_SESSION['last_page'] = $target_page; 
         $_SESSION['msg'] = "Something you wish to say about logging back and setting to users last page visited";
         // handle unset
         // Build your entire http path URL.

         $optional = '?key=value';// use `?id=` maybe '?id=."$id;
         $url = 'http://' . $_SERVER['HTTP_HOST']; // Get the server
         $url .= rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); // Get the current directory
         $url .= $target_page.$optional;  // <-- Your relative path with a success post through url            
         header('Location: ' . $url, true, 302);
         exit;
    }
}

这篇关于登录后将用户重定向到他们已退出的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆