Amjos.com Amjos.com - 6 months ago 16
PHP Question

How to fix session not starting after unsetting a variable

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');
}
?>

Answer

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

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