I have a login system that I've built with cookies. When I first log in, the cookie is generated and shown in the Chrome Cookies in the settings (That is how I know it has really been created). When I am transferred to dashboard, I can see the username I've logged in with and so far so good. The problem happens if I close the browser and go directly to the dashboard. It shows the username as undefined index until I manualy login again.
This is the creation of the cookie:
$_SESSION['userid'] = $row['userID'];
$_SESSION['username'] = $row['username'];
$cookie_value = $_SESSION['username'] ;
setcookie('guysite', $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
$_SESSION['userpwd'] = $row['password'];
$_SESSION['uname'] = $row['firstname'];
header("Location: dashboard.php");
And this is the dashboard check:
<?php
session_start();
if(!isset($_COOKIE['guysite']))
{
echo '<script>document.location.href="logout.php";</script>';
}
else
{
echo '<span class="loginstat">שלום: ' . $_SESSION['username'] . '</span></li><li><a href="logout.php" class="nohover">( <i class="fas fa-sign-out-alt"></i> התנתק )</a>';
}
?>