自定义session处理 关于read write顺序问题
header("content-type:text/html;charset=utf-8");
define('SESSION_PRE', 'XX_');
define('BR', '
');
function start ($savePath , $sessionName) {
global $sessionPath;
$sessionPath = $savePath . DIRECTORY_SEPARATOR;
echo __METHOD__ . BR;
return true;
}
function close () {
echo __METHOD__ . BR;
return true;
}
function read ($sessionId) {
global $sessionPath, $sessionFile;
$sessionFile = $sessionPath . DIRECTORY_SEPARATOR . ESSION_PRE . $sessionId;
echo __METHOD__ . BR;
return @file_get_contents($sessionFile);
}
function write ($sessionId , $data) {
global $sessionFile;
$sessionFile = $sessionPath . DIRECTORY_SEPARATOR . ESSION_PRE . $sessionId;
echo __METHOD__ . BR;
return @file_put_contents($sessionFile,$data);
}
function destroy ($sessionId) {
global $sessionFile;
@unlink($sessionFile);
echo __METHOD__ . BR;
return true;
}
function gc ($maxLifeTime) {
echo __METHOD__ . BR;
global $sessionPath;
foreach(glob($sessionPath."/*") as $file){
if (filemtime($file)+$maxLifeTime @unlink($file);
}
}
return true;
}
session_set_save_handler("start", "close", "read", "write", "destroy", "gc");
session_start(); //101行
$_SESSION['name'] = 'Lielei'; //102行
print_r($_SESSION); //103行
?>
问题描述:
按我的理解session_start()开启后,依次运行的函数是 start , read, write , close。
官方资料:PHP 会在脚本执行完毕或调用 session_write_close() 函数之后调用write函数。
那么,在第一次运行此脚本到103行时,为什么会输出Lilei呢,此时$_SESSION中的数据是
从session文件中读取出来的吗?如果是,脚本不是还没有结束吗,怎么会执行write呢?如果不是,
此时的$_SESSION只是相当于普通的数组吗?103行不过是输出一个普通的数组而已嘛?
回复讨论(解决方案)
求解释
依次输出
start
read
Array ( [name] => Lielei )
write
close
怎么不对呢?
print_r($_SESSION); 后就是 ?>
这不就表示程序结束了吗?
$_SESSION 是超全局数组,无需声明就可出现在程序的任何地方
依次输出
start
read
Array ( [name] => Lielei )
write
close
怎么不对呢?
print_r($_SESSION); 后就是 ?>
这不就表示程序结束了吗?
$_SESSION 是超全局数组,无需声明就可出现在程序的任何地方
如果print_r($_SESSON);后面再加几条语句呢。
我的意思是第一次运行 程序运行到103行 ,加入下面还有代码,怎么$_session理面就有值了?
1、$_SESSON 在 session_start(); 执行时生成
2、你在 print_r($_SESSION); 后面加几个 echo 'xxxx'; 不就看到了吗
3、你都 $_SESSION['name'] = 'Lielei'; 了,$_SESSION 怎么会没有值呢?
1、$_SESSON 在 session_start(); 执行时生成
2、你在 print_r($_SESSION); 后面加几个 echo 'xxxx'; 不就看到了吗
3、你都 $_SESSION['name'] = 'Lielei'; 了,$_SESSION 怎么会没有值呢?
我就是想搞清楚它的内部执行原理,session_start()后执行了多个函数,我看官方是:PHP 会在脚本执行完毕或调用 session_write_close() 函数之后调用write函数。
那么先执行了 print,然后才是程序结束,我说的是这个执行流程,既然先执行了print,那么还没有执行write,怎么就输出了呢
我不懂啊
1、$_SESSON 在 session_start(); 执行时生成
2、你在 print_r($_SESSION); 后面加几个 echo 'xxxx'; 不就看到了吗
3、你都 $_SESSION['name'] = 'Lielei'; 了,$_SESSION 怎么会没有值呢?
我就是想搞清楚它的内部执行原理,session_start()后执行了多个函数,我看官方是:PHP 会在脚本执行完毕或调用 session_write_close() 函数之后调用write函数。
那么先执行了 print,然后才是程序结束,我说的是这个执行流程,既然先执行了print,那么还没有执行write,怎么就输出了呢
我不懂啊
$_SESSION是存在内存中的,session_write_close()是将session数据写入服务器端上的session文件的。所以在写入文件之前,你对session的取值和设置,都是针对$_SESSION这个变量的。
?面未?束,不??行session_write_close(); 所以不?更新到session文件,你?取的只是?存中的session的值。
嗯嗯,我猜也是这样,但只是猜测,所以来问下,谢谢回答

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

In this article, we're going to explore the notification system in the Laravel web framework. The notification system in Laravel allows you to send notifications to users over different channels. Today, we'll discuss how you can send notifications ov
