RE: [PHP] Sessions
| From: | Justin Palmer | Date: | Mon, 26 Apr 2004 22:55:54 +0000 |
| Subject: | RE: [PHP] Sessions | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-184703@lists.php.net to get a copy of this message | ||
Hi,
What I normally do is carry a session object that holds many things
about the current user that is logged in(username, ipaddress, firstname,
lastname, (It all depends on how much you know about the user)). The
first page takes in a username and password that sends you to a second
page (login.php).
Within, login.php, you would have something like:
if($user->login($_POST['txtUsername'], $_POST['txtPassword']))
{
session_start();
$_SESSION['user'] = $user;
header('location:content page');
}
else
{
header('location:login page');
};
If the user fails to login never start the session, but if they do
succeed then we will do a check on every page from then on that looks
like this:
require_once(path to $user object);
session_start();
if(!is_object($user)) header('location:login page');
Since $user is an object when we register it, it will always be an
object, unless over written,in which case we make the user login again.
$user should never get overwritten though.
I hope this helps.
Regards,
Justin Palmer
-----Original Message-----
From: Daniel Clark [mailto:dclark@nwlink.com]
Sent: Monday, April 26, 2004 9:18 AM
To: H Bartel
Cc: php-general@lists.php.net
Subject: Re: [PHP] Sessions
I usually do something like this.
Check if session variable exists, and check if it has a name set.
session_start();
if ( !isset($_SESSION['username')) || $_SESSION['username'] == '') {
header('Location: login.php');
}
> 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
> } ?>
>
> 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.
>
> Thanks in advance for your suggestions & help,
> Holger
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php