如何在取消设置变量后修复会话未启动

时间:2016-05-09 10:30:43

标签: php session indexing

我一直试图解决这个问题,但不适合我。我已经开始会话但在使用unset($_SESSION['variable']注销后,由于会话未设置,我无法返回。

  

请帮帮我。注意:取消设置变量后我无法登录。

//this is login.php
<?php
    session_start();
    ob_start();
    $msg = "";
    if (isset($_POST['login'])){
        include "dbconnect.php";
        if($_SESSION['emaill'] !== NULL){
            $emaill= $_SESSION['emaill'];
            $passWord = $_POST['passWord'];
            if (empty($emaill && $passWord) == false){
                $sql = "SELECT * FROM `sono` WHERE `eMail`='$emaill' && `passWord`='$passWord'";
                $check = mysqli_query($dbconnect, $sql) or die (mysqli_error($dbconnect));
                $result = mysqli_num_rows($check);
                if ($result > 0){
                    $emaill= $_SESSION['emaill'];
                    header ('location: home.php');
                    exit();
                } else
                    $msg = "<p style='color: red; padding-left: 10px'>Invalid password or email address</p>";
            } else
                $msg = "<p style='color: red; padding-left: 10px'>Please enter your email and password to login</p>";
        } else
            $msg = "<p style='color: red; padding-left: 10px'>My scripts not working yet</p>";
    }
?>

// this is for logout
<?php
    session_start();
    if (isset($_SESSION['emaill'])){
        unset($_SESSION['emaill']);
        header('location: login.php');
    }
?>

3 个答案:

答案 0 :(得分:3)

在使用$ _SESSION [&#39;电子邮件&#39;]之前,请使用isset()进行检查,如:

if( isset($_SESSION['emaill']) && $_SESSION['emaill'] !== NULL){
----
---
}

答案 1 :(得分:0)

添加isset点击此处

if(isset($_SESSION['emaill']) && $_SESSION['emaill'] !== NULL){

答案 2 :(得分:0)

您可以在SESSION之前使用空函数来检查值是否存在。由于在注销时未设置或销毁变量,因此无法在登录时识别SESSION。您可以尝试如下检查: -

if(!empty($_SESSION['emaill']))
{

// Your code here 

}