0

Please I have been trying to fix this but not working out right for me. I have a session started but after logging out using unset($_SESSION['variable'], I am unable to login back as the session is not setting.

Please help me out. Note: I am unable to login after unsetting the 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');
    }
?>
Amjos.com
  • 83
  • 10
  • 3
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Charlotte Dunois May 09 '16 at 10:32
  • 2
    In logout, you destroy `$_SESSION['emaill']` but when you attempt to login again, you dont check for the existance of `email` in line 8 ` if($_SESSION['emaill'] !== NULL){` before attempting to use it – RiggsFolly May 09 '16 at 10:38

3 Answers3

3

before using $_SESSION['emaill'], check it with isset() like :

if( isset($_SESSION['emaill']) && $_SESSION['emaill'] !== NULL){
----
---
}
Ranjit Shinde
  • 1,121
  • 1
  • 7
  • 22
  • 1
    Thanks Rahjith. i have tested the email, it's no more existing. It's not giving me undefined index again but the login query is not working. Showing the last error above I asigned in the script. $msg = "

    My scripts not working yet

    ";
    – Amjos.com May 09 '16 at 10:45
  • echo your sql and try to run it in mysql directly and check what error it showing – Ranjit Shinde May 09 '16 at 10:47
  • After echoing sql, It's showing Notice: Undefined variable: sql in C:\xampp\htdocs\buth_net\nursingSchool\login.php on line 22 and when I run it from mysql, it returning emptry result. I think the major now is that, session is not starting after logging out. – Amjos.com May 09 '16 at 11:24
0

Add isset check here

if(isset($_SESSION['emaill']) && $_SESSION['emaill'] !== NULL){
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
0

You can use the empty function before SESSION to check the value exists or not . As the variable is being unset or destroyed at the time of logout it is unable to recognize the SESSION at the time of login . You can try checking like this below :-

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

// Your code here 

}
Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
Megan Fox
  • 435
  • 2
  • 6
  • 20