| AND OR | string words | *

sessions and co

|

sessions class


session class for an easy session handling.


function Session( ) [ line 6 ]


function set_var( ) [ line 12 ]


function get_var( ) [ line 38 ]


function get_sid_string( ) [ line 70 ]


function get_sid( ) [ line 76 ]


function var_unset( ) [ line 82 ]


function ses_unset( ) [ line 111 ]


function destroy( ) [ line 135 ]


function show( ) [ line 142 ]



full source of sessions class [ line 1 - 167 ] | download sessions class

1    <?php
2   class Session
3   {
4       // Start a new session
5       // Syntax: $session = new session();
6       function Session()
7       {
8           session_start();
9       }
10       // Set a variable in a current session
11       // Syntax: $session->set_var("Var name", "Value");
12       function set_var($varname,$varvalue)
13       {
14           if(!isset($varname) || !isset($varvalue) )
15           {
16               die('Function setvar( String $varname, mixed $value ) expects two parameters!');
17           }
18           if(phpversion() >= "4.1.0")
19           {
20               $_SESSION[$varname] = $varvalue;
21           }
22           if(!isset($GLOBALS[$varname]) )
23           {
24               $GLOBALS[$varname] = $varvalue;
25           }else{
26               global $HTTP_SESSION_VARS;
27               session_register($varname);
28               $GLOBALS['HTTP_SESSION_VARS'][$varname] = $varvalue;
29               if(!isset($GLOBALS[$varname]) )
30               {
31                   $GLOBALS[$varname] = $varvalue;
32               }
33           }
34       }
35       // Get a variable from current session
36       // Syntax: $variable = $session->get_var("var name");
37       // echo "$variable" to get grabbed session variable
38       function get_var$varname )
39       {
40           if(!isset($varname))
41           {
42               die('Function getvar( String $varname ) expects a parameter!');
43           }
44           ifphpversion() >= "4.1.0" )
45           { 
46               if (isset($GLOBALS[$varname]))
47               { 
48                   return $GLOBALS[$varname];
49               }
50               elseif (isset($GLOBALS['_SESSION'][$varname]))
51               {
52                   $GLOBALS[$varname] = $GLOBALS['_SESSION'][$varname];
53                   return $GLOBALS['_SESSION'][$varname];
54               }
55           }else{
56               if (isset($GLOBALS[$varname]))
57               { 
58                   return $GLOBALS[$varname];
59               }
60               elseif (isset($GLOBALS['HTTP_SESSION_VARS'][$varname]))
61               {
62                   $GLOBALS[$varname] = $GLOBALS['HTTP_SESSION_VARS'][$varname];
63                   return $GLOBALS['HTTP_SESSION_VARS'][$varname];
64               }
65           }
66       }
67       // Get a current session string (EXAMPLE: PHPSESSID=b188e8c9c45b347cdded2)
68       // Syntax: $sid = $session->get_sid_string();
69       // echo "$sid" to grab the string id.  By default it uses PHPSESSID= variable
70       function get_sid_string()
71       {
72           return session_name() . "=" session_id();
73       }
74       // Get current session ID (EXAMPLE: whatever=b188e8c9c45b347cdded2)
75       // Syntax: $sid = $session->get_sid();
76       function get_sid()
77       {
78           return session_id();
79       }
80       // Unset a current session variable
81       // Syntax: $session->var_unset("variable name");
82       function var_unset($varname)
83       {
84           if(!isset($varname))
85           {
86               die('Function var_unset( String $varname ) expects a parameter!');
87           }
88           ifphpversion() >= '4.1.0')
89           {
90               if(isset($GLOBALS[$varname]))
91               {
92                   unset($GLOBALS[$varname] );
93               }
94               if (isset($GLOBALS['_SESSION'][$varname]))
95               { 
96                   unset($GLOBALS['_SESSION'][$varname]);
97               }
98           }else{
99               if (isset($GLOBALS[$varname]))
100               { 
101                   unset($GLOBALS[$varname] );
102               }
103               if (isset($GLOBALS['HTTP_SESSION_VARS'][$varname]))
104               {
105                   unset($GLOBALS['HTTP_SESSION_VARS'][$varname]);
106               }
107           }
108       }
109       // Unset every variable in the current session 
110       // Syntax: $session->ses_unset();
111       function ses_unset()
112       {
113           if(phpversion() >= '4.1.0')
114           {
115               if(isset($GLOBALS['_SESSION'])) $a $GLOBALS['_SESSION'];
116               {
117                   while(list($key,) = each($a))
118                   {
119                       $this->var_unset($key);
120                   }
121               }
122           }else{
123               if(isset($GLOBALS['HTTP_SESSION_VARS']))
124               {
125                   $a $GLOBALS['HTTP_SESSION_VARS'];
126                   while(list($key,) = each($a))
127                   {
128                       $this->var_unset($key);
129                   }
130               }
131           }
132       }
133       // This will delete your current session and delete every session variable created
134       // Syntax: $session->destroy();
135       function destroy()
136       {
137           $this->ses_unset();
138           session_destroy();
139       }
140       // Display all variables in a current session
141       // Syntax: $session->show();
142       function show()
143       {
144           echo 'Variables set in current session:<br>';
145           if(phpversion() >= '4.1.0')
146           {
147               if(isset($GLOBALS['_SESSION']))
148               {
149                   $a $GLOBALS['_SESSION'];
150                   while(list($key,$value) = each($a))
151                   {
152                       echo '<b>Variable:</b> '.$key.' - <b>Value:</b> '.$value.'<br>';
153                   }
154               }
155           } else {
156               if(isset($GLOBALS['HTTP_SESSION_VARS']))
157               {
158                   $a $GLOBALS['HTTP_SESSION_VARS'];
159                   while(list($key,$value) = each($a))
160                   {
161                       echo '<b>Variable:</b> '.$key.' - <b>Value:</b> '.$value.'<br>';
162                   }
163               }
164           }
165       }
166   }
167   ?>



18 hits by 10 users in the last 30 minutes.