用户在注销后没有重新回到上次访问的页面

时间:2018-05-05 09:01:44

标签: php redirect cookies

在我的网站上,我试图将用户重新定向到他们重新登录后访问的最后一页,但我没有得到它。我使用cookie来跟踪他们上次访问过的页面,我创建了一个重定向页面来检查cookie是否仍然存在,如果确实存在,应该将它们带到他们中断的最后一页。

这是我网站的标题,我将其包含在所有页面中:

include('../includes/admins.php');  

// Set your cookie before redirecting to the login page
$current_page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$_COOKIE['redirect_to'] = $current_page;

$cookie_name = $_COOKIE['redirect_to'];
$cookie_value = $current_page;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
     <meta name="description" content="Automating your school work.">
    <meta name="author" content="<?php echo $set['siteName']; ?>">
    <meta name="keyword" content="content, management, system, schools, school management system, schoool portal, chat, link, linkedln, <?php echo $set['siteName']; ?>">
    <link rel="icon"  href="<?php echo $set['installUrl'].'logo/'.$set['schoolLogo']; ?>" type="image" />
    <title><?php echo $fullname;  ?></title>
    <!-- Favicon-->
    <link rel="apple-touch-icon" sizes="57x57" href="images/apple-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="images/apple-icon-60x60.png">
     <link rel="manifest" href="images/manifest.json">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="msapplication-TileImage" content="images/ms-icon-144x144.png">
    <meta name="theme-color" content="">
    <!-- Google Fonts -->
    <link href="../afiles/css/font.css" rel="stylesheet" type="text/css">
    <link href="../afiles/css/icon.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <link href="../afiles/css/font-awesome.min.css" rel="stylesheet"  type="text/css">
    <!-- Bootstrap Core Css -->
    <link href="../afiles/plugins/bootstrap/css/bootstrap.css" rel="stylesheet">
    <link rel="stylesheet" href="../afiles/css/bootstrap-material-datetimepicker.css" />
</head>

如果你看顶部,你会看到我包含了cookie。

以下是重定向页面,以查看是否存在Cookie:

<?php
include('../includes/functions.php');

if(!isset($_COOKIE[$cookie_name])) {
    header("Location: main");
} else {
    header("Location: ". $cookie_name);
}

?>

需要有人指出我正确的方向。

1 个答案:

答案 0 :(得分:0)

在第二段代码中,您有$_COOKIE[$cookie_name]但由于您尚未在文件中定义$cookie_name!isset($_COOKIE[$cookie_name])将始终返回false。可能的解决方案:

替换

if(!isset($_COOKIE[$cookie_name])) {

if(!isset($_COOKIE['redirect_to'])) {

匹配第一个块代码的索引值。

如果您认为可以更改redirect_to值,则第二个选项是define一个单独文件中的常量,然后在两个块中使用它。