I read many other topics about this but still can't figure out what's my problem. It was working, then my computer crashed e now it seems it's not memorizing the session variable anymore. So this is my code
the login page, where I create the session (if the user is authenticated)
<?php session_start();
if(!empty($_POST['subject'])) //SPAM
exit;
include "functions.php";
$con=Connection();
$usr = mysqli_fetch_array(mysqli_query($con, "SELECT usrname, usrpw FROM users"));
if(password_verify($_POST['usrname'], $usr[0]) && password_verify($_POST['usrpw'], $usr[1])){
session_regenerate_id(true);
echo "crea session";
$_SESSION['logged'] = hash('sha256', 'L9oT8s5iF3yX1uW');
$_SESSION['remote_ip'] = $_SERVER['REMOTE_ADDR'];
$_SESSION['year'] = date('Y');
echo "<h12>welcome!<br/><br/>";
echo"<a href='home.php'>Home</a></h12>";
}else{
session_destroy();
echo "<h12wrong data.<br/></br>";
echo"<a href='login1.php'>try again</a></h12>";
}
mysqli_close($con); ?>
<html>
<head>
<title> Login </title>
<link type="text/css" rel="stylesheet" href="css/styles.css" title="Style" media="all" />
</head>
</html>
these are the first lines of code in my "home" page after logging in (and creating the session variables). The other lines is html so I omitted it.
<?php session_start();
if(!isset($_SESSION['logged']) && $_SESSION['logged'] != 1)){
echo "not logged ".$_SESSION['logged'];
exit;
}
?>
The login page works (it goes inside the if stat. as the "create session" string will be printed), but as soon as I go to the home page It will throw this error: Notice: Undefined index: logged at line 2 (where I'm checking if the session variable isset). I tried commenting out all the session variables except 'logged' but still couldn't solve.
thanks
EDIT: sorry, about the parenthesis I just made a mistake writing here (I changed the string I had with the number 1 and I forgot to remove the parenthesis). Still not working.
I tried this and still throws the error (in fact the echo won't print anything after "not logged ")
if(!isset($_SESSION['logged'])){
echo "not logged ".$_SESSION['logged'];
exit;
}
EDIT 2 : I deleted all the code in home.php and made it like this:
<?php session_start();
echo $_SESSION['logged'];
?>
it will still tell me that 'logged' index is undefined
)to much in the if statement (at the end) (Also i would use ||)password_verifyfor the username)