RE: Avoid to open mysql querries then times in the page

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 

> -----Original Message-----
> From: tedd [mailto:tedd.sperling@xxxxxxxxx] 
> Sent: Tuesday, May 05, 2009 7:55 AM
> To: php-general@xxxxxxxxxxxxx
> Subject: Re:  Avoid to open mysql querries then times in the page
> 
> 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

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
OOP.

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
anyways.

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 ) { ... }


D.Vin
http://daevid.com





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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux