Jump to content

How many SESSIONs is too many?


tgavin

Recommended Posts

I'm re-factoring my app and am looking at using sessions instead of constants.

 

Currently, I have about 25 or so constants that define different parameters of the app. This has been working OK, but I'm starting to run into some problems with expansion, so thought sessions would be perfect. I'd just put them into an array, like so

 

<?php
$_SESSION['template'] = array();
$_SESSION['template']['name'] = $row_template['template_name'];
$_SESSION['template']['auto_format'] = $row_template['auto_format'];
?>

 

This is working just fine. However, I'm wondering about performance. If I start storing all these items in sessions, how much can I do before the server, or user starts experiencing problems, or performance issues?

 

Am I better off storing them in arrays? Or should I register each setting as its own session?

Link to comment
Share on other sites

Unfortunately I'm encountering problems with certain aspects of the app because it needs to remain flexible. One major drawback has been the server path to the site root. I can set this in one part of the app, like a config file, but because of includes and user-defined templates this has been a major nightmare. Storing that in a session has been perfect.

 

I know that it's easy to say this is a design flaw, but I have not come across anything that will allow me to point to the site root, then to a particular sub-directory IF the user has defined one. So, storing items in sessions has really proven quite flexible for me.

 

If there's a better way I'm all open to hearing about it. That's why I designed everything in constants to begin with - so that I wouldn't have to worry about global vars, functions, etc.

 

thanks :)

Link to comment
Share on other sites

  • 2 weeks later...

Whenever creating a new php file I have 2 variables.

 

$root = "../"; - to root index directory

$element = "../element"; - to include() files directory

 

Whenever I need to include() a file, I use include(($element).'filename.php');

 

In the actual file that I included (filename.php) I might need to reference to an image or something... so I use $root

Link to comment
Share on other sites

 

5 session variables is "too many". Just because you are having trouble with paths doesn't mean you should get sloppy with sessions.

 

 

Here is a line out of my system that defines the root dir from TWO levels down in the "/site/config.php" file:

<?php

// Absolute file system path to the CX root
define('SITE_DIR', realpath(dirname(__FILE__). "/../../"));

?>

Link to comment
Share on other sites

  • 3 weeks later...

Sorry if this is pulling up a too old thread, but this is quite relevant to me atm.

 

So, you said 5 session variables is "too many"? How come? Why is this too many?

 

I am working on a project, that uses ALOT of session variables, ( i might say around 50, might be a little less ). The project is very poorly planned and done to begin with, but that really is quite the only way of storing information from page to page, or atleast, the only one I am familiar with. The project has more than 120 .php files ( yes, 115 of them kinda repeats it self :) ) and have a 'path info', like what you have selected previously stored in sessions and so on. I havent really seen any sluggish performance on that, though I havent benchmarked at all.

 

So, other than the fact that it is really messy, I dont see a problem using alot of session variables. Maybe someone could prove me wrong?

 

ps. I have started to rewrite the whole project to have 5 different files, and generate forms and such, instead of putting them all into a different files :)

Link to comment
Share on other sites

I quess I did :)

 

I was wondering if there were any other reason in it, that I am not aware of. Now, would you still consider it to be messy, if you store the information, for example, my 50 session variables, into a, lets say 4 or 5 session arrays? Or can you think of a way to store that information some other way, than sessions, database or files :)

Link to comment
Share on other sites

If these settings aren't user specific there is no point storing them in the $_SESSION array. Sessions are specifically designed to store data relating to a particular user.

Almost, they are specific to the currently active session. Users change within the same session.
Link to comment
Share on other sites

I'd be interested to here any comments on the performance side of session vars. Let's assume that there are occasionally good reason for using a lot of session vars, is this going to have adverse performance implications? Does PHP cache the vars on the server, or is there a HDD read each time the var is accessed in a script?

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.