Mega Code Archive

 
Categories / Php / MySQL Database
 

This functions makes it easy to use session-variables known from ASP

With one Cookie the array session will save and restore from a db-record <?php # This Include handle Session based variable handling # # Please feel free and use it. If you make it more functional # it would be nice to send me a copy. # # Don't forget - Mysql_connect ! # # The database structure # Table structure for table 'session' # # CREATE TABLE session ( # id int(11) DEFAULT '0' NOT NULL auto_increment, # sid varchar(20) DEFAULT '' NOT NULL, # val blob, # times timestamp(14), # PRIMARY KEY (id), # KEY sid (sid), # UNIQUE sid_2 (sid) # ); # # You'll miss here a cron job to delete the old sessions from db $sess_db = 'test'; $sess_table = 'session'; # Session_CheckID - Get or Set the Session-ID # Parameter.: time how long the cookie will keept # or null if it's only a session cookie # Return....: Session-Unique ID function Session_CheckID( $min ) { global $sess_sid; if( !$sess_sid ) { $sess_sid = uniqid( SC ); if( $min > 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 # 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 <?php require( "session.inc" ); if( $del ) { session_del(); } session_checkid( 0 ); mysql_connect('') or Die("can't connect to db!"); # Normal use ist to read the session-var and assign it to the # working vars. This example assign after session-read fail the # new values. if( session_read() ) { $hallo = $session[hallo]; $w12 = $session[w12]; $arr = $session[arr]; } else { for( $i = 1; $i <= 10; $i++ ) { for( $j = 1; $j <= 2; $j++ ) { $arr[$i][$j] = $i+$j; } } $w12 = '10232'; $hallo = 'Ho oh. '; } ?> <html> <head><title>Session/Cookie-Test 1</title> </head> <body> <h2>This Page should show how to handle the "session.inc" library</h2> <h3>We will use a mask with a record showing routine</h3> <?php print "<h4>Show all variables</h4>"; for( $i = 1; $i <= 10; $i++ ) { print "\$arr: [$i][1-2] = " . $arr[$i][1] . " / " . $arr[$i][2] . "<br>"; } print "<br>"; print "w12: " . $w12 . "<br>"; print "hallo: " . $hallo . "<br>"; # 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; } ?> <form action=sess1.php3 method=post> <hr> <input type=submit name=del value=" reset session "> If you like to reset the session - click </form> </body> </html>