On 5/4/09, Matthieu <spamamis@xxxxxxxxx> wrote:
Hello,
I'm a totally newbie to php/Mysql but I'd like to know if it is normal that
I have to connect 3 times to the db in one page.
For example, I have
1. A connection for the login / pass a $_SESSION['login'] before the HTML
tags
2. I need to say hello to the user so I reconnect, run a query to select the
user having the login and echo 'Hello '.$user['login'].'!''
3. I need to show him his friends a bit later, so I have to connect a last
time and re-run a querry because I can't use the data $user that I used in
my upper php code...
Is there a walkthroug to have only one connection for the page?
Thanks
Matthieu
Matthieu:
The way I usually have a user navigate a protected site is to first
to have them identify themselves via a logon/password script -- and
then I store their user_id in a SESSION. Note, I do not store all
their data in a SESSION, just their user_id. The user_id should be an
unique auto_increment integer primary key from your users' table.
At the start of each protected page, I have:
<?php session_start();
$user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;
if($user_id == 0)
{
header('location: login.php);
exit();
}
// proceed with processing
As such, I check if $user_id > 0 -- if so, then I process the request
as directed. If not, then I send the user back to login.
As for connecting to the database, I connect as needed to get
information needed. I do not use SESSIONs to store all the data to be
passed from page to page, I gather only what's needed for that page.
I also make sure that when I open a connection, I close the
connection I may have several open/close statements within a page,
but normally I try to avoid that.
HTH's
tedd
--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php