Jump to content

php session


poe

Recommended Posts

how much can i store in a session.

 

i download a DB and store it in an array.

i then sort and modify the array so it is unique to the persons browser

 

i want to access that array throughout my entire site without having to download the DB every page,

can i sore the contents of the array into the session. and if so, is there a max size. or is there a better way to store this info?

 

thanks

Link to comment
Share on other sites

the max size will most likely be whatever the script memory_limit is. from what i gather, that sounds like what you want to do. problem is, the session wont last forever so if you want the user's settings to be saved, you'd have to write it back to the db at some point....

Link to comment
Share on other sites

What you are trying to do is extremely inefficient, and a misuse of databases. Databases are made to be accessed repeatedly. But getting information out of them that you don't need is inefficient.

 

Call the information you need from the database when you need it.

Link to comment
Share on other sites

What you are trying to do is extremely inefficient, and a misuse of databases. Databases are made to be accessed repeatedly. But getting information out of them that you don't need is inefficient.

 

Call the information you need from the database when you need it.

 

i certainly agree, but to each his own...

Link to comment
Share on other sites

hmmm.. how about instead of downloading to an array that gets stored as a session - download tha data to an array, and put back to a page table inthe db instaed. is that a better way of doing things?

Link to comment
Share on other sites

Hi,

 

it seems you are too concerned about going to DB to get something you want when you need. you like to save the db connection, fetch time cost at any rate even though its takes relatively fraction of time considering the advantage we have now in terms of cheap processing units now available.

 

DBs are supposedly made to organize the information much better than we do ourself in array and supposedly made to retrieve information quickly rather than doing our own way through array. saving date in array holds good only in case if you have very less amount of data. but this will be defeated once your data grows and php certainly is not best to handle huge data structures and its not php duty either. so best leave the job to mysql for what its made for.

 

few suggestions to you to avoid unnecessary DB connections, DB queries, processing upon DB return result set are, use singleton pattern to instantiate DB connection in order to avoid multiple DB connections, try to cache the query result whenever and wherever possible to avoid firing repeated queries to DB by employing a good coding practice and thirdly if you have an application that does lots of business logic processing over the DB query result set, cache the processed information either as serialized form in DB or session, this saves query execution time and business logic processing time over and again.

 

hope this gives you some leads

 

"Happy Coding"

Ramchel

Link to comment
Share on other sites

thanks...

 

i am only looking at a db with a max of 100 records or so.

 

what i wanted to acheive is:

 

my template table : each record is the name of a different template

tpl_a, tpl_b, tpl_c, etc...

 

each template, is a different html layout / theme (i am using smarty).

 

my other table is of links.

 

so i want to get a list of templates, randomly sort, then this will be the order of templates used when a person pages through my site.

 

ie tpl_a, the results are shown in 1 column, tpl_b the results are shown in 2 columns, tpl_c the results are shown in 3 columns, etc... or each template is a different color scheme..

 

so... when someone comes to my site...

 

tpl_ary = select tpl_name from templates

shuffle(tpl_ary)

 

ie. (tpl_ary now looks like tpl_c tpl_d tpl_a tpl_b)

 

 

links = select * from links limit 0,10

 

$show_tpl = $tpl_ary[0];

$smarty->display($show_tpl);

 

so when the person clicks next page(show next 10 records)

 

links = select * from links limit 11,10

 

*** REMEMBER tpl_ary ***

*** this is when i saved this into a session ***

*** however, after x minutes the session dies and i loose this info ***

*** which is why i thought saving in a db would be better ***

 

now i pull the tpl_ary string from the saved_session table

turn back into an array

and i get the next value along

 

$show_tpl = $tpl_ary[1];

$smarty->display($show_tpl);

 

so each person who comes to my site will page through the links the same way, but will see them of different 'themed' templates in different orders.

 

 

i dont want to use get random tpl from db every page load, because if person is on page 5 and clicks to see page 1, i want it to remember what them page 1 was originally

 

so bottom line:

 

person A:

page 1 - link 1-10 : 2 column layout with a blue page theme(tpl_c)

page 2 - link 11-20 : 1 column layout with a red page them (tpl_f)

page 3 - link 21-31 : 2 column layout with a green page theme(tpl_a)

 

person B:

page 1 - link 1-10 : 1 column layout with a pink page theme(tpl_g)

page 2 - link 11-20 : 2 column layout with a green page theme(tpl_a)

page 3 - link 21-31 : 2 column layout with a brown page theme(tpl_b)

so is person B now clicks page 1 link again it will show:

page 1 - link 1-10 : 1 column layout with a pink page theme(tpl_g)- ok

instead of (if i used random tpl)

page 1 - link 1-10 : 2 column layout with a gold page theme(tpl_m)- not ok

 

 

i hope this clarifies a bit better of what i am trying to acheive.

 

 

 

Link to comment
Share on other sites

Hi,

 

so you basically want differently themed pages whenever a user go to a new page and when he comes to an already visited page you want retain the previously shown theme.

 

fine analyse the following suggestions and decide how much you can take from it,

 

first create the cache table with following structure to cache some query results, theme selection preference etc..

 

CREATE TABLE cache(session_id, cache_data);

 

I will write an algorithm kind of solution with which you can build the program suitable to your application,

 

STEP 1: retrieve all available templates from your DB and form an array named say arrTemplate

STEP 2: choose a template randomly from the array and store the chosen template id in a variable say intTemplateID

STEP 3: display the page with all the details and with the template we just chosen

STEP 4: now we need to find a way to get a unique page id for the pages user visits so that we need to retain the same theme we have shown before

STEP4-SUB-STEP 1: best way of finding unique page id is to use the REQUEST_URI of $_SERVER

STEP4-SUB-STEP 2: so create an array variable named say arrTemplatePref and have REQUEST_URI as index and intTemplateID as value to create unique representation for the view user is in

STEP 5: now almost after showing the page do the last bit of processing.

STEP5-SUB-STEP 1: create an array named say arrCacheData

STEP5-SUB-STEP 2: store the arrTemplate in arrCacheData

STEP5-SUB-STEP 3: store the arrTemplatePref in sub array of arrCacheData, remember this has to be in sub array because we may need to push some more list of user views into this.

so finally this arrCacheData might look like this

arrCacheData => Array(
			'TEMPLATE_INFO' => Array(1, 2, 3, 4, 5, 6),
			'USER_VIEWS'	=> Array(
							'view.php?limit=20&limitstart=50&contentid=23434' => 3,
							'view.php?limit=20&limitstart=70&contentid=23434' => 1
						)
		)

STEP 6: this is time to store the arrCacheData into the table 'cache' we created with 'session_id' as hook. you can serialize the information before storing.

 

Final Implementation: At each page loads, query the cache table to check whether there is any cached data available. if so loads it in an array. At STEP 2, check the cache array before firing the query because remember we store the information in the cache table. At STEP 3, before choosing the template randomly, check the cache array USER_VIEWS sub array using the REQUEST_URI to check whether the user has already visiting the page or not and if so you can retreive the template ID and set that template as current template. thats it we have got a complete cycle of using the cache to storing the cache.

 

I'm not sure how cleary I have defined my solution(today is my second day at PHPFreaks so not sure of forum rules) but I'm sure by this you can acheive what you are trying to do.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.