home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / sessionmanagement.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  7.7 KB  |  277 lines

  1. Session-Management 
  2.  
  3. This functions make it easy to use session-variables known from ASP. With one Cookie the array "session" will save and restore from a db-record. In this version MySQL is used but it's should very easy to change. 
  4.  
  5. <?php 
  6. # ------------------------------------------------------------------- 
  7. # Session Management v1.0 21.6.1998 
  8. # (c) Wild Karl Heinz <kh.wild@wicom.at> 
  9. # This Include handle Session based variable handling 
  10. # Please feel free and use it. If you make it more functional 
  11. # it would be nice to send me a copy. 
  12. # Don't forget - Mysql_connect ! 
  13. # The database structure 
  14. # Table structure for table 'session' 
  15. #  CREATE TABLE session ( 
  16. #    id int(11) DEFAULT '0' NOT NULL auto_increment, 
  17. #    sid varchar(20) DEFAULT '' NOT NULL, 
  18. #    val blob, 
  19. #    times timestamp(14), 
  20. #    PRIMARY KEY (id), 
  21. #    KEY sid (sid), 
  22. #    UNIQUE sid_2 (sid) 
  23. #  ); 
  24. # You'll miss here a cron job to delete the old sessions from db 
  25. # ------------------------------------------------------------------- 
  26.  
  27.    $sess_db =  'test'; 
  28.    $sess_table =  'session'; 
  29.  
  30. # ---------------------------------------------------- 
  31. # Session_CheckID - Get or Set the Session-ID 
  32. # Parameter.: time how long the cookie will keept 
  33. #             or null if it's only a session cookie 
  34. # Return....: Session-Unique ID 
  35. # ---------------------------------------------------- 
  36. function Session_CheckID( $min ) 
  37.    global $sess_sid; 
  38.  
  39.    if( !$sess_sid ) { 
  40.       $sess_sid = uniqid( SC ); 
  41.       if( $min > 0 ) { 
  42.          SetCookie( "sess_sid", $sess_sid, time()+($min*60),  "/",  "", 0 ); 
  43.          } 
  44.       else { 
  45.          SetCookie( "sess_sid", $sess_sid,  "",  "/",  "", 0 ); 
  46.          } 
  47.       return( false ); 
  48.       } 
  49.    else { 
  50.       return( $true ); 
  51.       } 
  52.  
  53. # ---------------------------------------------------------- 
  54. # str2arr - build out from a string with eval the new array 
  55. # parameter.: string 
  56. # returns...: global array 
  57. # ---------------------------------------------------------- 
  58. function str2arr( $ts ) 
  59.    global $session; 
  60.  
  61.    $vals = split(  "&", $ts ); 
  62.    while( list($key,$val) = each($vals) ) { 
  63.       list( $name, $wert ) = split(  "=", $val ); 
  64.       if( $val ) eval(  "\$$name = \"$wert\";" ); 
  65.       } 
  66.  
  67. # ---------------------------------------------------------- 
  68. # session_read - reads the session-variables 
  69. # Parameter.: none 
  70. # returns...: read - ok = true 
  71. # ---------------------------------------------------------- 
  72. function session_read() 
  73.     # Hash array to keep session-variables 
  74.    global $session; 
  75.  
  76.    global $sess_sid, $sess_db, $sess_table, $sess_error; 
  77.  
  78.  
  79.    $sel =  "Select val from $sess_table where sid = '$sess_sid'"; 
  80.    $res = mysql_db_query( $sess_db, $sel ); 
  81.    if( mysql_numrows( $res ) ) { 
  82.       $val = mysql_result( $res, 0,  "val" ); 
  83.       str2arr( $val ); 
  84.       mysql_free_result( $res ); 
  85.       return( true ); 
  86.       } 
  87.    else { 
  88.       return( false ); 
  89.       $sess_error = mysql_error(); 
  90.       } 
  91.  
  92. # ------------------------------------------------------ 
  93. # Split_Array - reads the session-array into a string 
  94. # Parameter.: array 
  95. # returns...: string with & separeted array fields 
  96. # Thanks to Rasmus 
  97. # ------------------------------------------------------ 
  98. function Split_Array( $arr, $a =  "", $b =  "", $c =  "" ) 
  99.    while( list( $key, $val ) = each( $arr ) ) { 
  100.       if( is_array( $val ) ) { 
  101.          $ts .= Split_Array( $arr[ $key ], 
  102.                   ( strlen( $a ) ? $a : $key ), 
  103.                   ( strlen( $b ) ? $b : ( strlen( $a ) ? $key :  "" ) ), 
  104.                   ( strlen( $c ) ? $c : ( strlen( $b ) ? $key :  "" ) ) ); 
  105.          } 
  106.       else { 
  107.          $ts .=  "session"; 
  108.          $ts .= $a ?  "[$a]" :  ""; 
  109.          $ts .= $b ?  "[$b]" :  ""; 
  110.          $ts .= $c ?  "[$c]" :  ""; 
  111.          $ts .=  "[$key]=$val&"; 
  112.          } 
  113.       } 
  114.    return( $ts ); 
  115.  
  116. # --------------------------------------------------- 
  117. # session_write - writes the session-variable from 
  118. #                 the array session 
  119. # parameter.: none 
  120. # returns...: write - ok = true 
  121. # --------------------------------------------------- 
  122. function session_write() 
  123.     # Hash array to keep session-variables 
  124.    global $session; 
  125.  
  126.    global $sess_sid, $sess_db, $sess_table; 
  127.    global $sess_error; 
  128.  
  129.     # if you like to delete a session-cookie 
  130.     # you must check it before writting the session 
  131.     # array 
  132.  
  133.    if( !$sess_sid ) { session_checkid( 0 ); } 
  134.  
  135.    $ts = Split_Array( $session ); 
  136.    if( $ts >  "" ) { $ts = substr( $ts, 0, strlen( $ts ) - 1 ); } 
  137.    $sel =  "Select * from session where sid = '$sess_s'"; 
  138.    $res = mysql_db_query( $sess_db, $sel ); 
  139.    if( mysql_numrows( $res ) == 0 ) { 
  140.       $sel  =  "Insert into $sess_table ( id, sid, val, times ) "; 
  141.       $sel .=  "values( 0, '$sess_sid', '$ts', NULL )"; 
  142.       } 
  143.    else { 
  144.       $sel  =  "Update $sess_table set val = '$ts', "; 
  145.       $sel .=  "times = NULL where sid = '$sess_sid'"; 
  146.       } 
  147.    if( !mysql_db_query( $sess_db, $sel ) ) { 
  148.       $sess_error = mysql_error(); 
  149.       return( false ); 
  150.       } 
  151.    else { return( true ); } 
  152.  
  153. # --------------------------------------------- 
  154. # session_del - clears an entry 
  155. # parameter.: hash - id 
  156. # returns...: none 
  157. # --------------------------------------------- 
  158. function session_del() 
  159.    global $session, $sess_db, $sess_table, $sess_sid; 
  160.  
  161.    $sel =  "Delete from $sess_table where sid = '$sess_sid'"; 
  162.    if( !mysql_db_query( $sess_db, $sel ) ) { 
  163.       $sess_error = mysql_error(); 
  164.       } 
  165.    $sess_sid =  ''; 
  166.  
  167. ?> 
  168.  
  169. [------------------ This is an example ------------------] 
  170.  
  171. <?php 
  172.    require(  "session.inc" ); 
  173.  
  174.    if( $del ) { 
  175.       session_del(); 
  176.       } 
  177.  
  178.    session_checkid( 0 ); 
  179.  
  180.    mysql_connect( '') or Die( "can't connect to db!"); 
  181.  
  182.     # Normal use ist to read the session-var and assign it to the 
  183.     # working vars. This example assign after session-read fail the 
  184.     # new values. 
  185.    if( session_read() ) { 
  186.       $hallo = $session[hallo]; 
  187.       $w12 = $session[w12]; 
  188.       $arr = $session[arr]; 
  189.       } 
  190.    else { 
  191.       for( $i = 1; $i <= 10; $i++ ) { 
  192.          for( $j = 1; $j <= 2; $j++ ) { 
  193.             $arr[$i][$j] = $i+$j; 
  194.             } 
  195.          } 
  196.  
  197.       $w12 =  '10232'; 
  198.       $hallo =  'Ho oh. '; 
  199.       } 
  200.  
  201. ?> 
  202. <html> 
  203. <head><title>Session/Cookie-Test 1</title> 
  204. </head> 
  205.  
  206. <body> 
  207.  
  208. <h2>This Page should show how to handle the "session.inc" library</h2> 
  209.  
  210. <h3>We will use a mask with a record showing routine</h3> 
  211.  
  212. <?php 
  213.    print  "<h4>Show all variables</h4>"; 
  214.    for( $i = 1; $i <= 10; $i++ ) { 
  215.       print  "\$arr: [$i][1-2] = " . $arr[$i][1] .  " / " . $arr[$i][2] .  "<br>"; 
  216.       } 
  217.    print  "<br>"; 
  218.    print  "w12: " . $w12 .  "<br>"; 
  219.    print  "hallo: " . $hallo .  "<br>"; 
  220.  
  221.     # increment variables 
  222.    for( $i = 1; $i <= 10; $i++ ) { 
  223.       for( $j = 1; $j <= 2; $j++ ) { 
  224.          $arr[$i][$j] += 2; 
  225.          } 
  226.       } 
  227.  
  228.    $w12++; 
  229.    $hallo .=  "w1"; 
  230.  
  231.     # ------------------------------------- 
  232.     # reassign session variables 
  233.     # ------------------------------------- 
  234.    $session[arr] = $arr; 
  235.    $session[w12] = $w12; 
  236.    $session[hallo] = $hallo; 
  237.  
  238.     # ------------------------------------- 
  239.     # store session variables 
  240.     # ------------------------------------- 
  241.    if( !session_write() ) { 
  242.       print $sess_error; 
  243.       } 
  244.  
  245. ?> 
  246.  
  247. <form action=sess1.php3 method=post> 
  248. <hr> 
  249.    <input type=submit name=del value=" reset session "> 
  250.  
  251. If you like to reset the session - click 
  252.  
  253. </form> 
  254.  
  255. </body> 
  256. </html> 
  257.  
  258.