Re: using require

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

 



On Fri, October 14, 2005 11:33 am, Cima said:
> i have my web site working something like this: in every php script i have
> require(auth.php). this auth.php has my connection to my postgresql server
> and database along with some other stuff i need for the user to be
> authenticated to my web site. when i log on, this auth.php connects to the
> dbserver and checks if my username and password are stored and then i go
> to a home page. my connection is stored in $dbh.
> what happens when i start moving through all these web pages (php
> scripts), each requires auth.php, with respect to the connection? is a new
> connection established for every web page i go into that uses my $dbh for
> querying purposes or is it the same connection i originally made when i
> first logged into the web site?

You'll get a new connection on each page.

Which is good, because database connections CANNOT be shared across page
hits.

If you use _pconnect, you can get a "refurbished" connection from the
database instead of a truly new one.  But there are "gotchas" with that,
and I wouldn't recommend you jump into that without a lot more
research/experience.

I would suggest, however, that you put the database connection in a
totally separate file from the auth.php, and require them separately.

You may find more uses for your database some day, maybe even for pages
that do NOT require authentication, and you'll more easily do that if you
can just do:
<?php require 'db_connect.php';?>

For the pages that need authentication, you'd do:
<?php
  require 'db_connect.php';
  require 'auth.php';
?>

You could also look into http://php.net/require_once, but I tend to find
that people who start off with that end up being sloppy coders and end up
having a whole rats' nest of includes with no real Plan behind them, which
cause problems in the long run.  Just my opinion, and I'm bound to take
flak for it.

-- 
Like Music?
http://l-i-e.com/artists.htm

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