Welcome Guest, Not a member yet? Register   Sign In
Managing common data across your controllers - whats the best way?
#1

[eluser]phester[/eluser]
Hi,
I have a dilemma. Let's say you have a user that logs into an application. The application fetches details such as their name, telephone number, address, preferences, etc...

I use this information practically in over 75% of the pages in my application. It does not make sense to me to query this information from the database for every page. One solution, is to load this information into a cookie, and access the cookie instead of the database when the information is needed. I do not really like this idea as cookies are limited in size.

I was wondering if there is a way to somehow query this data once, and then dynamically create global variables such that I can access the information anywhere, anytime and in any controller. I would also like to know how to free these variables once the user logs out.

This is the behavior that I'd like:
Code:
$this->frequently_used_data->username
...or
Code:
$frequently_used_data['username']
...with the ability to access this information anywhere anytime while the user is logged on.

Any help is greatly appreciated! I know someone out there has the answer!

Thanks in advance,
#2

[eluser]sparkling tux[/eluser]
To store info of a logged in users I use session. For some reason it works better than cookies for me.

Quote:to access this information anywhere anytime
Unfortunately, because of the nature of web protocols, all data (not stored somewhere) is lost once the page is reloaded. (Correct me if I'm wrong.) Thus a session, cookies or a database is left.
#3

[eluser]Michael Wales[/eluser]
PHP is an instant-based language. Once the page is done loading, PHP is done - nothing more will ever occur to that page and it won't talk to the rest of your app. When you click a link, it's a whole new instance of the application with no prior knowledge of anything that has happened in the past.

Therefore, it is impossible to pass data around in a global sense like you are describing. Now - there are some ways to pass data from one instance of PHP to another, which I am sure you are already aware of: Cookies, Sessions, POST, and GET.

POST and GET is absolutely not the correct way to do this, so I won't even entertain the thought of explaining it. Don't do it.

Cookies and Sessions are good, but as you mentioned they are limited in space - and to be honest, what's the point?

One extra query isn't going to hurt your users that much - if you did a benchmark you might see a .05ms increase in load time thanks to that query, but I doubt it. So, the reasoning behind making this information easily accessible is for your own sake (and there's nothing wrong with that). Save yourself some time - right?

Why not store a Session variable that either contains the user's ID or FALSE (if they are not logged in). Then create a model which is loaded from every Controller's constructor. This model will query the database if the session variable !== FALSE, and pull in the user's username/email/etc. into an easy to use object (or array, whatever you want) for you.

Thus, you get your easy access to all of the user data you want, without having to duplicate your effort.
#4

[eluser]@li[/eluser]
The method walesmd suggested is a good solution. Another alternative is for you to use database-based sessions. Then, there would be no limit to the amount of data that can be stored in the session. Also when the user logouts or closes his browser, the session data would automatically be deleted. I suggest you look at the Obsessions class:
http://bleakview.orgfree.com/obsession/
#5

[eluser]Michael Wales[/eluser]
OBSession is a great alternative as well - I always forget about those third-party solutions because I have no need for them in my own work.

Even using OBSession he's adding another query to his page-loads. It would be virtually the same as my suggestion (rather than query by user_id, he would be querying by session_id - without any effort on his part though).

Either way is a good solution, although OBSession may provide more of what you are looking for from an "it just works" standpoint.




Theme © iAndrew 2016 - Forum software by © MyBB