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的值。
嗯嗯,我猜也是这样,但只是猜测,所以来问下,谢谢回答

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
