By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,225 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,225 IT Pros & Developers. It's quick & easy.

$_SESSION[] or $my_array[]

TheServant
Expert 100+
P: 1,168
Hi guys,
This is my situation. I have 3 sets of data used on every page of my website. Two of these never change, and the reason they are stored in MySQL and recalled into the $_SESSION variable is simply because I want to have one place to update/change them if I ever need to.
What I have been wondering is if I should leave the user specific variables in the session, and define the other variables in another file/array which is called on every page. This makes it easier to manage, less code, and no MySQL, but possibly more arrays.

So right now I have something like (which is only run once):
Expand|Select|Wrap|Line Numbers
  1. /*MySQL get table 1 variables*/
  2. /*Store each fetched variable into $_SESSION*/
  3. /*MySQL get table 2 variables*/
  4. /*Store each fetched variable into $_SESSION*/
  5. /*MySQL get table 3 variables*/
  6. /*Store each fetched variable into $_SESSION*/
  7.  
  8. /*Do stuff with $_SESSION['table1var1'], $_SESSION['table2var3'], $_SESSION['table1var2'], $_SESSION['table3var16']*/
Instead I was thinking:
Expand|Select|Wrap|Line Numbers
  1. /*Do once: MySQL get table 1 variables*/
  2. /*Do once: Store each fetched variable into $_SESSION*/
  3. /*Do every page: include table2.php*/
  4. /*Do every page: include table3.php*/
  5. ...
The included files could be in the form:
Expand|Select|Wrap|Line Numbers
  1. $table2['var1'] = 10;
  2. $table2['var2'] = "John";
  3. $table2['var3'] = 19/10/1986;
  4.  
  5. ***OR***
  6.  
  7. $table2_var1 = 10;
  8. $table2_var2 = "John";
  9. $table2_var3 = 19/10/1986;
I would like to do the 1st option but I wonder if more arrays will be noticeably slower, and if doing this on every page will also slow down the page load? Should I just leave it all in the $_SESSION array? Any advice would be greatly appreciated.
Mar 8 '09 #1
Share this Question
Share on Google+
4 Replies


100+
P: 136
I don't have expart view in this issue, but I prefer to use the 2nd process of using include files. In php version 4.x I had experienced the slow page loading due to storage of lot of variables in the session. From that time onwords I used to store only most req. variables in the session. Other variables I use to put in files (in the form of array) or directly fetch them from database.

I am very much interested to know other experts opinion. I feel this is a interesting isssue to discuss.
Mar 9 '09 #2

TheServant
Expert 100+
P: 1,168
Thanks for your reply. Just confirming: You saw a visible speed change when you changed from storing everything in the session to other methods?
Mar 9 '09 #3

100+
P: 136
@TheServant
Yes, there was good change in speed. Earlier I was putting approx 80 - 90 variables(mixed datatype) in session. I reduced the no. almost to 5-7. Some of the variable I put in the files and some of them I fetched from the database.

I was doing some complex processing and there ware many times I wasn't getting the desired result or I was experiencing the page time out. After I made those changes my scripts were running properly and I got expected results.
Mar 12 '09 #4

TheServant
Expert 100+
P: 1,168
Thanks for that. I have changed my method to only include constantly variable information in the session (which is still ~50 variables), and all the rest are stored in separate 'variable files' which are called by only the pages that need them. I do not store them in arrays, but rather straight variables.

I have another question which you might have come across: Every page I need to check if there has been an event to notify the user and also replace any outdated data in the session with the new data. This means that every page I am connecting to MySQL and running atleast one query. On some pages I only need maybe 10% of my $_SESSION variables, and I have always wondered if it would be faster to only keep user_id in my $_SESSION, and then call only required values from MySQL every page?

To sum up, is it faster to have a $_SESSION with 50 vars and query 7 values, or $_SESSION with 1 var and average query ~15 values?
Mar 12 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.