php的 session深入研究,更改session文件前序名。

我装的是xampp session文件保存在这里 C:\xampp\tmp

$sess_save_path = $sess_session_name = $fp=null;
function open ($save_path, $session_name) {
  global $sess_save_path, $sess_session_name;
       
  $sess_save_path = $save_path;
  $sess_session_name = $session_name;
  return(true);
}

function close() {
  global $fp;
  flock($fp, LOCK_UN);
  fclose($fp);
  $fp=null;
  return(true);
}

function read ($id) {
  global $sess_save_path, $sess_session_name, $fp;

  $sess_file = "$sess_save_path/saiye_$id";
  if ($fp = @fopen($sess_file, "r+")) {
    flock($fp, LOCK_EX);
    $sess_data = fread($fp, filesize($sess_file));
    return($sess_data);
  } else {
    return(""); // Must return "" here.
  }

}

function write ($id, $sess_data) {
  global $sess_save_path, $sess_session_name, $fp;

  $sess_file = "$sess_save_path/saiye_$id";
  if (!empty($fp)) {
    fseek($fp,0);
    return(fwrite($fp, $sess_data));
  } elseif ($fp = @fopen($sess_file, "w")) {
    flock($fp, LOCK_EX);
    return(fwrite($fp, $sess_data));
  } else {
    return(false);
  }
}

function destroy ($id) {
  global $sess_save_path, $sess_session_name;
       
  $sess_file = "$sess_save_path/saiye_$id";
  return(@unlink($sess_file));
}

function gc ($maxlifetime) {
    global $sess_save_path, $sess_session_name;
       
  $sess_file = "$sess_save_path/saiye_$id";
  return true;
}

session_set_save_handler ("open", "close", "read", "write", "destroy", "gc");

session_start();
$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();
print_r($_SESSION);
print_r($_COOKIE);
echo session_name();

?>