> -----Original Message-----
> From: tedd [mailto:tedd.sperl...@gmail.com] 
> Sent: Tuesday, May 05, 2009 7:55 AM
> To: php-general@lists.php.net
> Subject: Re: [PHP] Avoid to open mysql querries then times in the page
> On 5/4/09, Matthieu <spama...@gmail.com> 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

I would disagree with this. I strongly believe you should use a
User.class.php and store that instance in the $_SESSION. This gives you the
ID, name, login name, login state, roles, and every other thing you could
want to know about a user on a given page in a handy to access object. In
fact, a User class is one of those things that really benefits from being

The only trick here Matthieu is to make sure you
require_once('User.class.php') BEFORE you session_start(); or it won't work.

Opening and closing the connections is also IMHO a bad idea. The connection
is one of the slowest parts. In mySQL it's not so bad, but it's very narrow
sighted to think in terms of only one RDBMS. Other RDBMS like Oracle for
example have a very slow connection. Why not just keep it around. Your page
only lasts a few seconds and will automatically close when completed

If you're writing a command line tool like a daemon, then that's another
story, but I doubt the OP is doing that.

Lastly, why all the extra variable, assignment and checking stuff here?

        $user_id = isset($_SESSION['user_id']) ? $_SESSION['user_id'] : 0;

Why not just this?

        if( intval($_SESSION['user_id']) < 1 ) { ... }

What I normally do is set a $_SESSION['login'] = true; 

A boolean is always faster to check and in your code you could do this:

        if ( !$_SESSION['login'] ) { ... }

or if you're really paranoid use the '===':

        if ( $_SESSION['login'] !== true ) { ... }


PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to