dongxu7408 2017-04-13 09:05
浏览 40
已采纳

注销时,PHP索引页面会发出会话未知错误

I have a login and logout session for my index page.

So, it uses the email of user as session, and when user logs in, it gets the details from fb using his email which is stored in session.

But when the user logs out, it shows an error of

Notice: Undefined index: email in D:\xampp\htdocs\site\index.php

My code goes as:

<?php 
    //15 2 2015
    session_start();
?>
<?php 
    $email = $_SESSION['email'];
    $sql = "SELECT * FROM landlords WHERE email = '$email' ";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {                                
    while($row = $result->fetch_assoc()) {                                   
?>
<?php 
        echo '<li class="dropdown submenu">';
        echo '<a href="#" class="dropdown-toggle">'; echo ($row["name"]); echo '</a>';
        echo '<ul class="dropdown-menu">';
        echo '<li><a href="myaccount.php">My Account</a></li>';
        echo '<li><a href="logout.php">Log Out</a></li>';
      echo '</ul>';
        echo '</li>';
?>
<?php    }
             } else {
        echo '<li>  <a href="signin.php"><span class="icon-user"></span>Sign In</a></li>';
        }
?>

Ignore the multiple php openings.

So, when a user logs in, it works fine, but when a user logs out, it gives a error of

Notice: Undefined index: email in D:\xampp\htdocs\site\index.php

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • doufan9395 2017-04-13 09:48
    关注

    The issue is that you don't guard the situation where the email session var is unset, as is after session unset (logout). Try this:

    $email = @$_SESSION['email'];
    if (!empty($email)) {
        $sql = "SELECT * FROM landlords WHERE email = '$email' ";
        $result = $conn->query($sql);
    }
    if (isset($result) && $result->num_rows > 0) {
    

    ...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配