session class for an easy session handling.

sessions class” makes use of the built-in PHP functions session_start( ), phpversion( ), session_name( ), session_id( ), each( ) and session_destroy( ).

001 <?php
002 
003 class Session
004 {
005     
006     // Start a new session
007     // Syntax: $session = new session();
008     
009     function Session()
010     {
011         session_start();
012     }
013             
014     // Set a variable in a current session
015     // Syntax: $session->set_var("Var name", "Value");
016     
017     function set_var($varname,$varvalue)
018     {
019         if(!isset($varname) || !isset($varvalue) )
020         {
021             die('Function setvar( String $varname, mixed $value ) expects two parameters!');
022         }
023         if(phpversion() >= "4.1.0")
024         {
025             $_SESSION[$varname] = $varvalue;
026         }
027         if(!isset($GLOBALS[$varname]) )
028         {
029             $GLOBALS[$varname] = $varvalue;
030         }else{
031             global $HTTP_SESSION_VARS;
032             session_register($varname);
033             $GLOBALS['HTTP_SESSION_VARS'][$varname] = $varvalue;
034             if(!isset($GLOBALS[$varname]) )
035             {
036                 $GLOBALS[$varname] = $varvalue;
037     
038             }
039         }
040     }
041             
042     // Get a variable from current session
043     // Syntax: $variable = $session->get_var("var name");
044     // echo "$variable" to get grabbed session variable
045     
046     function get_var( $varname )
047     {
048         if(!isset($varname))
049         {
050             die('Function getvar( String $varname ) expects a parameter!');
051         }
052         if( phpversion() >= "4.1.0" )
053         { 
054             if (isset($GLOBALS[$varname]))
055             { 
056                 return $GLOBALS[$varname];
057             }
058             elseif (isset($GLOBALS['_SESSION'][$varname]))
059             {
060                 $GLOBALS[$varname] = $GLOBALS['_SESSION'][$varname];
061                 return $GLOBALS['_SESSION'][$varname];
062             }
063         }else{
064             if (isset($GLOBALS[$varname]))
065             { 
066                 return $GLOBALS[$varname];
067             }
068             elseif (isset($GLOBALS['HTTP_SESSION_VARS'][$varname]))
069             {
070                 $GLOBALS[$varname] = $GLOBALS['HTTP_SESSION_VARS'][$varname];
071                 return $GLOBALS['HTTP_SESSION_VARS'][$varname];
072             }
073         }
074     }
075             
076     // Get a current session string (EXAMPLE: PHPSESSID=b188e8c9c45b347cdded2)
077     // Syntax: $sid = $session->get_sid_string();
078     // echo "$sid" to grab the string id.  By default it uses PHPSESSID= variable
079     
080     function get_sid_string()
081     {
082         return session_name() . "=" . session_id();
083     }
084     
085     // Get current session ID (EXAMPLE: whatever=b188e8c9c45b347cdded2)
086     // Syntax: $sid = $session->get_sid();
087     
088     function get_sid()
089     {
090         return session_id();
091     }
092     
093     // Unset a current session variable
094     // Syntax: $session->var_unset("variable name");
095     
096     function var_unset($varname)
097     {
098         if(!isset($varname))
099         {
100             die('Function var_unset( String $varname ) expects a parameter!');
101         }
102         if( phpversion() >= '4.1.0')
103         {
104             if(isset($GLOBALS[$varname]))
105             {
106                 unset($GLOBALS[$varname] );
107             }
108             if (isset($GLOBALS['_SESSION'][$varname]))
109             { 
110                 unset($GLOBALS['_SESSION'][$varname]);
111             }
112         }else{
113             if (isset($GLOBALS[$varname]))
114             { 
115                 unset($GLOBALS[$varname] );
116             }
117             if (isset($GLOBALS['HTTP_SESSION_VARS'][$varname]))
118             {
119                 unset($GLOBALS['HTTP_SESSION_VARS'][$varname]);
120             }
121         }
122     }
123     
124     // Unset every variable in the current session 
125     // Syntax: $session->ses_unset();
126     
127     function ses_unset()
128     {
129         if(phpversion() >= '4.1.0')
130         {
131             if(isset($GLOBALS['_SESSION'])) $a = $GLOBALS['_SESSION'];
132             {
133                 while(list($key,) = each($a))
134                 {
135                     $this->var_unset($key);
136                 }
137             }
138         }else{
139             if(isset($GLOBALS['HTTP_SESSION_VARS']))
140             {
141                 $a = $GLOBALS['HTTP_SESSION_VARS'];
142                 while(list($key,) = each($a))
143                 {
144                     $this->var_unset($key);
145                 }
146             }
147         }
148     }
149     
150     // This will delete your current session and delete every session variable created
151     // Syntax: $session->destroy();
152     
153     function destroy()
154     {
155         $this->ses_unset();
156         session_destroy();
157     }
158             
159     // Display all variables in a current session
160     // Syntax: $session->show();
161     
162     function show()
163     {
164         echo 'Variables set in current session:<br>';
165         if(phpversion() >= '4.1.0')
166         {
167             if(isset($GLOBALS['_SESSION']))
168             {
169                 $a = $GLOBALS['_SESSION'];
170                 while(list($key,$value) = each($a))
171                 {
172                     echo '<b>Variable:</b> '.$key.' - <b>Value:</b> '.$value.'<br>';
173                 }
174             }
175         } else {
176             if(isset($GLOBALS['HTTP_SESSION_VARS']))
177             {
178                 $a = $GLOBALS['HTTP_SESSION_VARS'];
179                 while(list($key,$value) = each($a))
180                 {
181                     echo '<b>Variable:</b> '.$key.' - <b>Value:</b> '.$value.'<br>';
182                 }
183             }
184         }
185     }
186 }
187  
188 ?>