Reprocess cookie or dump variables to session variables?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Reprocess cookie or dump variables to session variables?

Post by JAB Creations »

I'm working on my settings PHP class which from the last version of my site is just not something I think is salvageable.

So while I've been working on a much more refined way of figuring out all the visitor's preferences I'm curious: is it more efficient to execute PHP to parse the settings every single time or execute only when something changes and store the information in variables assigned to the visitor's session instead?

While far from complete here is what I'm messing with (just to help give a vague idea what PHP would be doing every single time if I don't end up storing the data in session variables)...

Code: Select all

<?php
 
$settings_default = array(
'audio'=>'0',
'broadbandimages'=>'0',
'checkbox'=>'0',
'columns'=>'1',
'connection'=>'0',
'css3'=>'0',
'csspatch'=>'1',
'cursors'=>'0',
'dhtmleffects'=>'0',
'dtd'=>'1',
'initialfocus'=>'search_query',
'keyboardlayout'=>'developer',
'mediatype'=>'ns',
'pagination'=>'form',
'personality'=>'0',
'powerkeys'=>'0',
'sidebar'=>'20',
'sounds'=>'0',
'theme'=>'classic'
);
 
foreach ($settings_default as $var=>$val)
{
 if (isset($_GET[$var])) {$$var = $_GET[$var];}
 else if (isset($_POST[$var])) {$$var = $_POST[$var];}
 else if (isset($_COOKIE['settings']))
 {
  $settings_cookie_pair = explode('_',$_COOKIE['settings']);
 
  foreach ($settings_cookie_pair as $var_c=>$val_c)
  {
   $var_c = explode('.',$val_c);
   $var1 = $var_c['0'];
   $$var1 = $var_c['1'];
  }
 }
 else
 {
  $$var = $val;
 }
}
?>
User avatar
Popcorn
Forum Commoner
Posts: 55
Joined: Fri Feb 21, 2003 5:19 am

Re: Reprocess cookie or dump variables to session variables?

Post by Popcorn »

if the user is given options to change the page without a server visit then don't the settings have to be in the cookie? even if you AJAX to update user settings on the server, subsequent user changes may need to refer to those same changes so you'd need them client-side.

and for any subsequent server visit, you may need to access those same settings on the server-side which, most simply, means reading the cookie somehow. after you've decided to read it, update whatever you need (i guess if other code that is a part of the request needs access to those settings, it'd be easier to ask that code to read a session var than a cookie).
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Reprocess cookie or dump variables to session variables?

Post by Jenk »

that's an awful lot of information to store in cookies.. surely they should be persistent values on a DB, and just a uid in the cookie?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Reprocess cookie or dump variables to session variables?

Post by JAB Creations »

Having options on my site has evolved the goal of handling data. I haven't yet directly worked with options on Version 2.9 (version in development) as I've been working on actual features (blog, forum, etc) so I'm slowly getting around to things like this now. I plan on having registered members have their preferences saved in MySQL while still having visitors have their preferences saved in a cookie.

I think that settings that are different from the default could be set to a session variable...and if the session variable is not set each setting could revert to it's default...which would be set in the file where the $settings class is set...and that would probably strike a nice balance.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Reprocess cookie or dump variables to session variables?

Post by Jenk »

Personally I'd still go for visitors having preferences in session only or DB with a uid. Probably with a cron job to clear out records older than 'x' days.
Post Reply