Re: Sessions

From: Date: Mon, 26 Apr 2004 14:11:27 +0000
Subject: Re: Sessions
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-184646@lists.php.net to get a copy of this message
H Bartel wrote:
Hi, I have recently started to use and understand Sessions in PHP. Starting and initiating a Session on Login works, but how do or can I check on the following pages, if the user is really logged in? For now I am using the following code, which seems to work: <?php session_start (); if (!$_SESSION) {
    header("Location: index.php"); // which directs back to login form
 } ?> 
Your if statement will always evaluate to true. Once you call session_start(), the global _SESSION array will exist even if it's empty.
Is this a correct way to do so, or is there some better or safer solution to this? Searched the archives as well, but couldn't find an appropriate answer.
When a user correctly logs in, you could set a session variable... if ( $whatever_login_checking ) { $_SESSION['logged'] = true; $_SESSION['user'] = $username; ... ... } And just check to see if that exists.... session_start(); if ( isset ( $_SESSION['logged'] ) && $_SESSION['logged'] ) { // user is logged in ...code... } else { // user is not logged in ...code... } -- *********************************************************************** * _ __ __ __ _ * John Nichel * * | |/ /___ __ \ \ / /__ _ _| |__ ___ __ ___ _ __ * 716.856.9675 * * | ' </ -_) _` \ \/\/ / _ \ '_| / /(_-<_/ _/ _ \ ' \ * 737 Main St. * * |_|\_\___\__, |\_/\_/\___/_| |_\_\/__(_)__\___/_|_|_|* Suite #150 * * |___/ * Buffalo, NY * * http://www.KegWorks.com john@KegWorks.com * 14203 - 1321 * ***********************************************************************

« previous php.general (#184646) next »