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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器