How can I do one login script that uses cookies for login and for example I want to check if the visitor is logged in without querying the database.
For example I want on frontpage to show some menu’s only for logged in users .
so I must do if(isLoggedIn()) show the menu . But that’s a query everytime the page loads so it’s not very good
Any suggestions?
You can do this:
session_start(); // Use $HTTP_SESSION_VARS with PHP 4.0.6 or less if (!isset($_SESSION['loggedIn'])) { $_SESSION['loggedIn'] = true; // Add all the relevant user information data $_SESSION['username'] = $username; $_SESSION['password'] = $password; $_SESSION['etc'] = $etc; }Then you can request the user data to the $_SESSION global array.
E.g.: requesting if the user is loggedIn (Don’t forget to call to session_start() first):
function isLoggedIn() { return (isset($_SESSION['loggedIn']) && $_SESSION['loggedIn']); }If you want to log out:
The timeout can be handled in the php.ini file:
Or you can handle ini variables in run time using ini_set:
ini_set('session.gc_maxlifetime', $sessionMaxLifeTime);