RE: [PHP] Another sessions question

From: Date: Wed, 30 Apr 2003 05:22:48 +0000
Subject: RE: [PHP] Another sessions question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-145731@lists.php.net to get a copy of this message
Ok, there's really no need to register errors in sessions. What you might want to do is something like this: <?php session_start(); if($_POST['login']) { if(!$r=$db->getOne("select id from users where user='{$_POST['user']}' && pass='{$_POST['pass']}'") errorOut("Invalid username"); $_SESION['logged_in']=$r; } if(!$_SESSION['logged_in']) showLogonForm(); <<<< WHATEVER THIS PAGE LOOKS LIKE >>>>> function errorOut($error) { echo "$error<br />\n"; exit; } Function showLogonForm() { echo "<form><input type=text name=username....etc."; } Basically what we're doing here is first checking to see if the user typed in the username/pass via ($_POST['login']) if they did, we query the database (or whatever you're doing) checking to see if the info's correct. If it is, we register the session var logged_in. If not, we display an error message and the page quits. After that, we're checking to see if they're logged on or not via $_SESSION['logged_in']. If they aren't, we make them log on by showing them the logon form. If they're logged on, we display our page. That's pretty much the basics of the kind of functionality you'd need for using sessions to logged in users. -----Original Message----- From: C.R. [mailto:odcs@rogers.com] Sent: Wednesday, April 30, 2003 9:55 PM To: PHP General Subject: [PHP] Another sessions question I have a login, registration, update, and a form if you forget your password. I am using sessions to control the login and error messages. So this is my problem When a user enters the login page a session is started, if the user enters the wrong login info they will get an error message which is set in a session variable called $error['whatever'] (this will change depending on the error). If the user then decides he should register first, and goes to the register page - he gets a message on the page that simply says 'Array'. This is because the session is still active and $error['whatever'] is still set, although I've cleared the array with - $error = array(); On each page I have something like if($error) { echo $error['whatever']; }. I have used done it this way to make things more uniform and understandable, but I may also have shot myself in the foot - is there a way to get around this without having to change the session variables for each of these pages. I hope I have explained this properly, or for that matter used sessions properly - I am still trying to learn about sessions and problems like this keep popping up. TIA -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.general (#145731) next »