Php > Cookies
This functions makes it easy to use session-variables known from ASP
This functions makes it easy to use session-variables known from ASP 0 ) { SetCookie("sess_sid", $sess_sid, time()+($min*60), "/", "", 0 ); } else { SetCookie("sess_sid", $sess_sid, "", "/", "", 0 ); } return( false ); } else { return( $true ); } } # ---------------------------------------------------------- # str2arr - build out from a string with eval the new array # parameter.: string # returns...: global array # ---------------------------------------------------------- function str2arr( $ts ) { global $session; $vals = split( "&", $ts ); while( list($key,$val) = each($vals) ) { list( $name, $wert ) = split( "=", $val ); if( $val ) eval( "\$$name = \"$wert\";" ); } } # ---------------------------------------------------------- # session_read - reads the session-variables # Parameter.: none # returns...: read - ok = true # ---------------------------------------------------------- function session_read() { # Hash array to keep session-variables global $session; global $sess_sid, $sess_db, $sess_table, $sess_error; $sel = "Select val from $sess_table where sid = '$sess_sid'"; $res = mysql_db_query( $sess_db, $sel ); if( mysql_numrows( $res ) ) { $val = mysql_result( $res, 0, "val" ); str2arr( $val ); mysql_free_result( $res ); return( true ); } else { return( false ); $sess_error = mysql_error(); } } # ------------------------------------------------------ # Split_Array - reads the session-array into a string # Parameter.: array # returns...: string with & separeted array fields # # Thanks to Rasmus # ------------------------------------------------------ function Split_Array( $arr, $a = "", $b = "", $c = "" ) { while( list( $key, $val ) = each( $arr ) ) { if( is_array( $val ) ) { $ts .= Split_Array( $arr[ $key ], ( strlen( $a ) ? $a : $key ), ( strlen( $b ) ? $b : ( strlen( $a ) ? $key : "" ) ), ( strlen( $c ) ? $c : ( strlen( $b ) ? $key : "" ) ) ); } else { $ts .= "session"; $ts .= $a ? "[$a]" : ""; $ts .= $b ? "[$b]" : ""; $ts .= $c ? "[$c]" : ""; $ts .= "[$key]=$val&"; } } return( $ts ); } # --------------------------------------------------- # session_write - writes the session-variable from # the array session # parameter.: none # returns...: write - ok = true # --------------------------------------------------- function session_write() { # Hash array to keep session-variables global $session; global $sess_sid, $sess_db, $sess_table; global $sess_error; # if you like to delete a session-cookie # you must check it before writting the session # array if( !$sess_sid ) { session_checkid( 0 ); } $ts = Split_Array( $session ); if( $ts > "" ) { $ts = substr( $ts, 0, strlen( $ts ) - 1 ); } $res = mysql_db_query( $sess_db, "Select * from session where sid = '$sess_s'"); if( mysql_numrows( $res ) == 0 ) { $sel = "Insert into $sess_table ( id, sid, val, times ) "; $sel .= "values( 0, '$sess_sid', '$ts', NULL )"; } else { $sel = "Update $sess_table set val = '$ts', "; $sel .= "times = NULL where sid = '$sess_sid'"; } if( !mysql_db_query( $sess_db, $sel ) ) { $sess_error = mysql_error(); return( false ); } else { return( true ); } } # --------------------------------------------- # session_del - clears an entry # parameter.: hash - id # returns...: none # --------------------------------------------- function session_del() { global $session, $sess_db, $sess_table, $sess_sid; $sel = "Delete from $sess_table where sid = '$sess_sid'"; if( !mysql_db_query( $sess_db, $sel ) ) { $sess_error = mysql_error(); } $sess_sid = ''; } ?> [------------------ This is an example ------------------]
Session/Cookie-Test 1
This Page should show how to handle the "session.inc" library
We will use a mask with a record showing routine
Show all variables"; for( $i = 1; $i <= 10; $i++ ) { print "\$arr: [$i][1-2] = " . $arr[$i][1] . " / " . $arr[$i][2] . "
"; } print "
"; print "w12: " . $w12 . "
"; print "hallo: " . $hallo . "
"; # increment variables for( $i = 1; $i <= 10; $i++ ) { for( $j = 1; $j <= 2; $j++ ) { $arr[$i][$j] += 2; } } $w12++; $hallo .= "w1"; # ------------------------------------- # reassign session variables # ------------------------------------- $session[arr] = $arr; $session[w12] = $w12; $session[hallo] = $hallo; # ------------------------------------- # store session variables # ------------------------------------- if( !session_write() ) { print $sess_error; } ?>
If you like to reset the session - click
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