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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?