1<?php 2 3DEFINE("SESSION_FILE_PREFIX" ,"session_test_"); 4function open($save_path, $session_name) { 5 global $session_save_path, $name; 6 $session_save_path = $save_path; 7 $name = $session_name; 8 echo "Open [${session_save_path},${session_name}]\n"; 9 return true; 10} 11 12function close() { 13 global $session_save_path, $name; 14 echo "Close [${session_save_path},${name}]\n"; 15 return true; 16} 17 18function read($id) { 19 global $session_save_path, $name, $session_id; 20 $session_id = $id; 21 echo "Read [${session_save_path},${id}]\n"; 22 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 23 // read MUST create file. Otherwise, strict mode will not work 24 touch($session_file); 25 return (string) @file_get_contents($session_file); 26} 27 28function write($id, $session_data) { 29 global $session_save_path, $name, $session_id; 30 $session_id = $id; 31 echo "Write [${session_save_path},${id},${session_data}]\n"; 32 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 33 if ($fp = fopen($session_file, "w")) { 34 $return = fwrite($fp, $session_data); 35 fclose($fp); 36 return $return === FALSE ? FALSE : TRUE; 37 } 38 return false; 39} 40 41function destroy($id) { 42 global $session_save_path, $name; 43 echo "Destroy [${session_save_path},${id}]\n"; 44 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 45 unlink($session_file); 46 return true; 47} 48 49function gc($maxlifetime) { 50 global $session_save_path, $name; 51 $directory = opendir($session_save_path."/"); 52 $length = strlen(SESSION_FILE_PREFIX); 53 while (($file = readdir($directory)) !== FALSE) { 54 $qualified = ($session_save_path."/".$file); 55 if (is_file($qualified) === TRUE) { 56 if (substr($file, 0, $length) === SESSION_FILE_PREFIX && (filemtime($qualified) + $maxlifetime <= time() )) { 57 unlink($qualified); 58 } 59 } 60 } 61 closedir($directory); 62 return true; 63} 64 65function create_sid() { 66 $id = ('PHPT-'.time()); 67 echo "CreateID [${id}]\n"; 68 return $id; 69} 70 71function validate_sid($id) { 72 global $session_save_path, $name; 73 echo "ValidateID [${session_save_path},${id}]\n"; 74 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 75 $ret = file_exists($session_file); 76 return $ret; 77} 78 79function update($id, $session_data) { 80 global $session_save_path, $name; 81 echo "Update [${session_save_path},${id}]\n"; 82 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 83 $ret = touch($session_file); 84 return $ret; 85} 86 87 88function feature() { 89 /* NOT IMPLEMENTED YET */ 90 /* TYPES: gc, create_sid, use_strict_mode, minizie_lock, lazy_write 91 /* VALUES: 0=unknown, 1=supported, 2=partially supported, 3=unsupported */ 92 return array('gc'=>0, 93 'create_sid'=>1, 94 'use_strict_mode'=>2, 95 'minimize_lock'=>3, 96 'lazy_write'=>4, 97 'invalid'=>5, 98 'another invalid'=>6 99 ); 100} 101 102?> 103 104