【问题标题】:PHP: user is successfully logout but after logout refresh home page it shows that undefined variable:PHP:用户已成功注销,但注销刷新主页后显示未定义的变量:
【发布时间】:2017-12-05 20:56:55
【问题描述】:

电子邮件是成功注销后的用户会话电子邮件,不存在电子邮件,但在索引页面上显示未定义的索引,因为注销后没有电子邮件。

Notice: Undefined index: email in C:\xampp\htdocs\CMS\index.php on line 9
    <?session_start();
?>
<?php
    require("conection/connect.php");   
    $username=$_SESSION['email'];
    $sel= "select * from users where email= '$username'";
    $run_name = mysqli_query($con, $sel);
    $row_user=mysqli_fetch_array($run_name);
    $name=$row_user['name'];
    $userid=$row_user['user_id'];
    $teacherid=$row_user['teacher_id'];
    $userid=$row_user['stu_id'];
    $usertype=$row_user['type'];
?>

【问题讨论】:

  • 如何执行注销?可能是通过取消会话?那么 $_SESSION 是一个空数组,访问 $_SESSION['email'] 会抛出一个警告
  • window.open('../index.php', '_self')"; ?>
  • 这是我的登出页面。代码。 window.open('../index.php', '_self')"; ?>
  • 那么你不应该访问 $_SESSION['email'] ;)
  • 如果登录用户关闭页面并进入主页,我已设置它。应该有一个已经登录的状态,这很好。但是注销后它会显示未定义索引的通知。

标签: php session undefined-variable


【解决方案1】:

只需verify that it's set。无论如何,这可能对你有用,因为我假设如果它没有设置你可能想做一些不同的事情。

<?php

if (!isset($_SESSION['email'])) {
    // maybe redirect to login?
    // or at least don't show them logged-in user type stuff
}

// your code (in an else block if you didn't redirect in the if)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2021-11-23
    • 2020-08-15
    相关资源
    最近更新 更多