Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for storing global data in PHP?

Tags:

php

mysql

session

I'm running a web application that allows a user to log in. The user can add/remove content to his/her 'library' which is displayed on a page called "library.php". Instead of querying the database for the contents of the users library everytime they load "library.php", I want to store it globally for PHP when the user logs in, so that the query is only run once. Is there a best practice for doing this? fx. storing their library in an array in a session?

Thanks for your time

like image 513
soren.qvist Avatar asked Mar 08 '10 14:03

soren.qvist


3 Answers

If you store each user's library in a $_SESSION as an array, as you suggested (which is definitely possible) you will have to make sure that any updates the user makes to the library are instantly reflected to that session variable.

Honestly, unless there is some seriously heavy querying going on to fetch a library, or you have tons of traffic, I would just stick to 'execute query whenever the user hits library.php'.

like image 177
karim79 Avatar answered Nov 18 '22 14:11

karim79


Consider the size of the data. Multiply that by the maximum number of concurrent users.

Then compare that the to memory avaiable on your server. Also consider whether or not this is a shared server; other sites needs resources too.

Based on this, it is probably best to either create a file that can be used (as per Remi's comment), or remain in the default stateless form and read every time. I doubt that reading the data each time is creating much of an overhead.

like image 2
Dave Avatar answered Nov 18 '22 16:11

Dave


When the user login you can generate a xml file (USER_ID.xml for instance) that you display with xslt.

http://php.net/manual/en/book.xslt.php

like image 1
remi bourgarel Avatar answered Nov 18 '22 16:11

remi bourgarel