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 return (string) @file_get_contents($session_file); 24} 25 26function write($id, $session_data) { 27 global $session_save_path, $name, $session_id; 28 $session_id = $id; 29 echo "Write [${session_save_path},${id},${session_data}]\n"; 30 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 31 if ($fp = fopen($session_file, "w")) { 32 $return = fwrite($fp, $session_data); 33 fclose($fp); 34 return $return; 35 } 36 return false; 37} 38 39function destroy($id) { 40 global $session_save_path, $name; 41 echo "Destroy [${session_save_path},${id}]\n"; 42 $session_file = "$session_save_path/".SESSION_FILE_PREFIX.$id; 43 return unlink($session_file); 44} 45 46function gc($maxlifetime) { 47 global $session_save_path, $name; 48 $directory = opendir($session_save_path."/"); 49 $length = strlen(SESSION_FILE_PREFIX); 50 while (($file = readdir($directory)) !== FALSE) { 51 $qualified = ($session_save_path."/".$file); 52 if (is_file($qualified) === TRUE) { 53 if (substr($file, 0, $length) === SESSION_FILE_PREFIX) { 54 unlink($qualified); 55 } 56 } 57 } 58 closedir($directory); 59 return true; 60} 61 62?> 63 64