Php > Miscellaneous
Session class
Session class start(); <-- Start a session. $foo->session([Session_id]); <-- Gives you the session_id. If a session_id is passed to the method, a new session will be started, using the given session_id (See PHP-function: session_id()) $foo->stats(); <-- Shows you information about the environment. (PHP-version, reg_globals) $foo->clear(); <-- Ends a session $foo->test_cookies(); <-- Returns true if cookies are allowed, false if not. Needs to be executed before any output is displayed. Because the page, needs to be reloaded. $foo->show(); <-- Shows all variables in a session $foo->show_errors(); <-- Shows you an error-log. (To check if theres something wrong with the values, given to the set-method.) $foo->clear_errors(); <-- Clears the error-log (No real use, but anyway) $foo->set(); <-- Registers a variable in a session. Values passed to the method, need to be: A pair (or a number of pairs) of "Name => Value", or an Array where the key will be used as the var-name and the value as the var-value. Example: $test = "text"; is to be registerd: objekt->set(VarName, VarValue); $foo->set("test", $test); (It's the same as: $foo->set("test", "text");) Another example: $foo->set("value1", 1, "value2", 4); Differences if register_globals = on/off: Example: $foo = 1; $bar->set("foo", 2); echo "foo = " . $foo; register_globals = on: Ausgabe: foo = 2; register_globals = off: Ausgabe: foo = 1; $foo->get(["_ARRAY"]); <-- Get a variable from a Session. If the string "_ARRAY" is passed, the method will return an Array with all session variables. Example: You want $x to have the value of the session-var $bar: var = objekt->get(SessionVarName); $x = $foo->get("bar"); */ class my_session{ var $rg; var $old; var $s_id; var $errors; function my_session($s = false){ if(ini_get("register_globals")) $this->rg = true; else $this->rg = false; if(phpversion() <= "4.1.0") $this->old = true; else $this->old = false; if($s == true) $this->start(); } function start(){ session_start(); $this->s_id = session_id(); } function sessionid($new_id = false){ if(!$new_id) return $this->s_id; else session_id($new_id); } function clear(){ session_destroy(); } function set(){ $v = func_get_args(); if(is_array($v[0])){ foreach($v[0] as $k => $value){ if($this->check_var_syntax($k)) $this->reg_vars($k, $value); } } else { if(count($v) % 2){ $this->errors[] = "Variable '" . $v[count($v) - 1] . "' ignored"; array_pop($v); } for($i = 0; $i < count($v); $i += 2){ if($this->check_var_syntax($v[$i])) $this->reg_vars($v[$i], $v[$i+1]); } } } function check_var_syntax($buff){ if(preg_match("!^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$!i", $buff)) return true; else { $this->errors[] = "Variable '
" . $buff . "
' ignored,
wrong syntax"; return false; } } function reg_vars($n, $v){ if($this->rg){ $GLOBALS[$n] = $v; session_register("$n"); } else { session_register("$n"); if($this->old) $GLOBALS['HTTP_SESSION_VARS'][$n] = $v; else $GLOBALS['_SESSION'][$n] = $v; } } function get($buff){ if($buff == "_ARRAY"){ if($this->old) return $GLOBALS['HTTP_SESSION_VARS']; else return $GLOBALS['_SESSION']; } if($this->old) return $GLOBALS['HTTP_SESSION_VARS'][$buff]; else return $GLOBALS['_SESSION'][$buff]; } function test_cookies(){ if($this->old) $url = $GLOBALS['HTTP_SERVER_VARS']['PHP_SELF'] . "?" . $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']; else $url = $GLOBALS['_SERVER']['PHP_SELF'] . "?" . $GLOBALS['_SERVER']['QUERY_STRING']; $start = $this->get("start"); if($start > 0){ switch($start){ case 1: setcookie("cookie", "1"); $this->set("start", 2); header("Location: $url"); break; case 2: if($this->old) ($GLOBALS['HTTP_COOKIE_VARS']['cookie']) ? ($cookies = true) : ($cookies = false); else ($GLOBALS['_COOKIE']['cookie']) ? ($cookies = true) : ($cookies = false); break; } } else { $this->set("start", 1); header("Location: $url&PHPSESSID=" . session_id()); } return $cookies; } function stats(){ $stats = array(); if($this->rg) $stats['register_globals'] = 1; else $stats['register_globals'] = 0; $stats['PHP-Version'] = phpversion(); $this->display("Description", "Value", $stats); } function show(){ if($this->old) $session_values = $GLOBALS['HTTP_SESSION_VARS']; else $session_values = $GLOBALS['_SESSION']; if(!is_array($session_values)) $session_values = array("No session-vars" => ""); $this->display("Name of the variable", "Value", $session_values); } function show_errors(){ if(!is_array($this->errors)) $this->errors[] = "No error"; $this->display("Description", "", $this->errors, "left"); } function clear_errors(){ $this->errors = ""; } function display($head_1, $head_2, $content, $align = "center"){ echo '
' . $head_1 . '
' . $head_2 . '
'; foreach($content as $k => $v){ ($i++ % 2) ? ($c = "#e0e0e0") : ($c = "#eeeeee"); echo '
' . $k . '
' . $v . '
'; } echo '
'; } } ?>
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage