代码之家  ›  专栏  ›  技术社区  ›  Tonye Boro

用户注销后未重新搜索回上次访问的页面

  •  2
  • Tonye Boro  · 技术社区  · 6 年前

    在我的网站中,我试图将用户重定向回他们重新登录后访问的最后一个页面,但我没有得到它。我使用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是否存在:

    <?php
    include('../includes/functions.php');
    
    if(!isset($_COOKIE[$cookie_name])) {
        header("Location: main");
    } else {
        header("Location: ". $cookie_name);
    }
    
    ?>
    

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gynteniuxas hrboy    6 年前

    在您的第二个代码块中 $_COOKIE[$cookie_name] 但既然你还没有定义 $cookie_name 在您的文件中, !isset($_COOKIE[$cookie_name]) 将始终返回 false .可能的解决方案:

    代替

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

    具有

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

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

    第二种选择,如果你认为你可能会改变 redirect_to 值,是到 define 在单独的文件中使用常量,然后在两个块中使用它。