Re: Beginner Sessions Question

From: Date: Fri, 28 Jun 2002 17:05:35 +0000
Subject: Re: Beginner Sessions Question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-104369@lists.php.net to get a copy of this message
> 1. I know that you initially begin the session with the session_start() function. Is this function required on all pages in order for the session variables to remain globalized? Yes. And the session_start(); function must come before any output. I ussually just put it right at the top of the script. > 2. If you have a "process login" page, which the user is sent to after submitting the login information, is this page ideal to hold the session_register() functions to define global variables and their values? That would be fine, but this is more a question of programming style than functionality. There is no standard method in terms of the structure of your login script. > 3. Would this piece of code be good to hold the preferences of a user that has just logged in? A few problems. First the lines where you check for the user's existance in the database are completely uncessary since you're doing the same thing again in your ELSE statment... so you're doing the same thing twice. You can use the retrieval of the user's preferences as a confirmation of their username and password. The other problem is that you need to define your variables before registering them in the session. Other than that I don't see anything wrong.. at least not a glance. Hope this helps. -Kevin ----- Original Message ----- From: "Martin Clifford" <MLC1@nrc.gov> To: <php-general@lists.php.net> Sent: Friday, June 28, 2002 10:54 AM Subject: [PHP] Beginner Sessions Question > Hello, everyone! > > I have been learning a little bit about sessions recently, and they seem to be an excellent tool to veer away from the statelessness of HTTP. I have a few questions about sessions though, which will seem like beginner questions to most of you, because they are. > > 1. I know that you initially begin the session with the session_start() function. Is this function required on all pages in order for the session variables to remain globalized? > > 2. If you have a "process login" page, which the user is sent to after submitting the login information, is this page ideal to hold the session_register() functions to define global variables and their values? > > 3. Would this piece of code be good to hold the preferences of a user that has just logged in? > > <?php > > session_start(); > $link = mysql_connect("localhost", "user", "pass") > or die("Could not connect to database."); > $db = mysql_db_select("users", $link) > or die("Could not select database."); > > $query = "SELECT user, pass FROM users WHERE user='$user' AND > pass='$pass' LIMIT 1"; > $result = mysql_query($query, $link) > or die("Query was not successful. " . mysql_error()); > if(mysql_num_rows($result) < 1) { > "User not found. Please try again."; > } else { > $query = "SELECT * FROM users WHERE user='$user' AND pass='$pass' > LIMIT 1"; > $result = mysql($query, $link) > or die("Query was not successful. " . mysql_error()); > while($row = mysql_fetch_array($result)) { > $name = $row['name']; > $colorpref = $row['colorpref']; > $fontpref = $row['fontpref']; > $sizepref = $row['sizepref']; > } > session_register("UserName"); $UserName = $name; > session_register("Color"); $Color = $colorpref; > session_register("Font"); $Font = $fontpref; > session_register("Size"); $Size = $sizepref; > } > > ?> > > I know it's a lengthy example, but I wrote it, and want to know whether or not that would work to load user preferences into the variables UserName, Color, Font and Size? > > That about does it for this installment of PHP Session Questions. Please reply directly since I'm on the digest! > > Thanks in advance, everyone! :o) > > Martin > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >

« previous php.general (#104369) next »