Page 1 of 1

Global Database Connection?

Posted: Thu Oct 19, 2006 10:08 am
by montecristo
Hi guys,

I have just started PHP, so please forgive me if my question is stupid. I am developing a simple customer application, where a user can log in and query a customer and the cutomer's purchases.

I have a database class which handles all database functionality e.g connects to the database etc. This class is called when a user logs on to the system. This validates the username and password, if these are corrrect it calls the query screen using

Code: Select all

header('location:queryuser.php');
Next a query screen appears allowing the user to query a customer id, which brings back the customer details from the database.

To get the details I have to call my database class again, connect and login into the database to retrieve the details.Surely there is another way to do this without creatng a new instance of the database class?

Any suggestions would be appreciated

Thanks

Posted: Thu Oct 19, 2006 10:15 am
by feyd
Each request to PHP is a separate instance, it has no relation to the previous request. So normally, a class is not able to persist across requests. You can store a class into a session variable, and there are shared memory options available too, but in this case, the normal course of action is the create another instance of the database class. I'd use session data to persist the user information so you don't have to request that they reauthenticate.

Also, relative path redirection isn't supported with some browsers, specifically standards complaint (only) ones. Use full URLs, http://domain and all..

Posted: Thu Oct 19, 2006 10:24 am
by Todd_Z
Storing the class in session is probably going to take up a lot of memory if you have a bunch of users on at any given time. Try to store a unique id which is assigned to each user in a session variable and then grab that info on each page load.

Posted: Thu Oct 19, 2006 10:35 am
by montecristo
Thanks alot guys,

Just had a quick look at session data, seems it might do the trick.

Thanks for the quick response!