note 92830 added to ref.session

From: Date: Tue, 11 Aug 2009 04:10:25 +0000
Subject: note 92830 added to ref.session
Groups: php.notes 
Request: Send a blank email to php-notes+get-159308@lists.php.net to get a copy of this message
I remember being confused by sessions when I first started programming so hopefully this simple class will help someone in clarifying a few points for them. I've found that the class structure is much smoother and lends itself to cleaner code than declaring $_SESSION over and over throughout your application. An object-oriented approach to handling your sessions will save you time and lend itself to more readily understandable code. <? class SessionManager { var $value; //multi //CREATE OR UPDATE A NEW SESSION VARIABLE function ModifyData($setKey, $setValue) { //Keys need to be strings if (is_string($setKey)) { $_SESSION[$setKey] = $setValue; } else { die("setKey must be a string!"); } } //REMOVES SESSION DATA FROM A SPECIFIC KEY - MUST BE STRING function ClearData($setKey) { if (is_string($setKey)) { unset($_SESSION[$setKey]); } else { die("setKey must be a string!"); } } function GetData($setKey) { if (is_string($setKey)) { $this->value = $_SESSION[$setKey]; return $this->value; } else { die("setKey must be a string!"); } } } ?> Then, in your code.... <? session_start(); include("session_manager.php"); //assuming you save your classes in seperate files $sm = new SessionManager; $sm->ModifyData("USERNAME", "jHancock"); //where USERNAME is the key to that particular entry and jHancock is what is stored in USERNAME //Grabs the data using your key $sm->GetData("USERNAME"); //Removes a particular entry using a key $sm->ClearData("USERNAME"): ?> Another good approach in session handling is to separate your session keys into three groups - Context, Temporary, and Volatile - especially if you're working large datasets and you are in and out of the session non-stop. Create a class that on key registration tacks on a "_c" "_t" or "_v" to each session key and then maintain a list of all items currently in the session. Context -> Persistent data that stays stored in the session (such as authentication). Temporary-> Data that only needs to be stored for a short time, such as an ID needed to grab information from a database or saving page information to be displayed later. Volatile-> Data that only needs to stay available for as long as the current page is open. With your session keys divided into three groups, establish rules and procedures on page loads to handle the dismissal of these groups. Example -> On page load loop through each key in the Volatile group and unset each key. Example -> Establish a rule (ex: user navigates to a different section of the site) that triggers the dumping of the Temporary session group. Example -> Empty the Context group on logout. Obviously these will need to be tailored to individual uses but overall the practice will lead to a much more efficient session structure and prevent session data from piling up on your server, which can be a huge problem when dealing with thousands of concurrent users. ---- Server IP: 208.69.120.35 Probable Submitter: 98.215.60.105 ---- Manual Page -- http://www.php.net/manual/en/ref.session.php Edit -- https://master.php.net/note/edit/92830 Del: integrated -- https://master.php.net/note/delete/92830/integrated Del: useless -- https://master.php.net/note/delete/92830/useless Del: bad code -- https://master.php.net/note/delete/92830/bad+code Del: spam -- https://master.php.net/note/delete/92830/spam Del: non-english -- https://master.php.net/note/delete/92830/non-english Del: in docs -- https://master.php.net/note/delete/92830/in+docs Del: other reasons-- https://master.php.net/note/delete/92830 Reject -- https://master.php.net/note/reject/92830 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159308) next »