Could I please run a general query past any experienced programmers regarding use of cookies?
I'm currently using a session to pass user id number from one page to another.
I'd like to ask if a cookie would be better.
I'm currently thinking if a user signs in to their account, then I'd like to store their user id for 24 hours, saving them time signing again.
If the same user re-visits the site within 24 hours, I would like to pull the user id from the cookie and store it in a variable or session.
At present, assuming the user has the correct username & password, and this is the first signin with in the 24 hours, I've written:-
Code: Select all
//Create cookie
setcookie("userid", $row['acc_id'], time() + (86400), "/"); // 86400 = 1 day - Acc id direct from the database
}
And on the homepage, I'd like to toggle the menu, so that if the user has not signed in, then I'd like the menu to display Sign In and Register. Otherwise display a menu with options to goto the dashboard or some other user options.
Code: Select all
<ul class="nav navbar-nav navbar-right">
<?php
if(!isset($_COOKIE['userid'])) {
// if(count($_COOKIE) <1) {
echo "<li class='active'><a href='/Guest/signin/logon.html'><i class='fas fa-key'></i> Sign In</a></li>";
echo "<li><a href='/register.php'><i class='far fa-registered'></i> Register</a></li>";
} else {
echo "<li class='dropdown'>";
echo "<a href='#' class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'> User";
echo "<span class='caret'></span>";
echo "</a>";
echo "<ul class='dropdown-menu'>";
echo "<li><a href='#'>Nav 1</a></li>";
echo "<li><a href='#'>Nav 2</a></li>";
echo "<li><a href='#'>Nav 3</a></li>";
echo "</ul>";
echo "</li>";
echo "<li><a href='/user/dashboard.php' class='nav-controller'><i class='fas fa-dashboard'></i>Dashboard</a></li>";
echo "<li><a href='/signout.php' class='nav-controller'><i class='fas fa-logout'></i>Signout</a></li>";
}
?>
</ul>
I'd be grateful if an experienced programmer could advise me, or write the code on my behalf.
Thank You.