简体   繁体   English

会话文件 - 什么时候才能完全创建?

[英]session file - when does it get created exactly?

I'm curious to know exactly when the session text file is created? 我很想知道会话文本文件的确切创建时间? An easy and surface level answer to that would probably be,.. well, when you use session_start(). 一个简单而表面层面的答案可能是,嗯,当你使用session_start()时。 But is it? 但是吗? Could it be all the way at the end when the php script ends? 当php脚本结束时,它可以一直到最后吗?

What makes me think that it may be at the end is that I know PHP would not write to the session file everytime you make a change to a session variables while the execution of the page goes on. 是什么让我觉得最终可能是因为我知道每当你对页面的执行继续进行会话变量的更改时,PHP都不会写入会话文件。 For example, if you got something like $_SESSION['x'] = $_SESSION['x'] + 1; 例如,如果你有类似$_SESSION['x'] = $_SESSION['x'] + 1; in a loop, the session file does NOT get modified at each and every iteration of the loop. 在一个循环中,会话文件并没有得到在每循环的每次迭代修改。 Only when the PHP scripts is done, then the PHP engine makes a change into the file, to store the final value of the $_SESSION['x'] . 只有当PHP脚本完成后,PHP引擎才会对文件进行更改,以存储$_SESSION['x']的最终值。

With the same taken of logic, I see no reason for PHP engine to create a file as soon as you call the session_start. 使用相同的逻辑,我认为没有理由让PHP引擎在调用session_start时立即创建文件。 It could just delay the process. 它可能只是推迟了这个过程。

What that entails is this; 这需要的是这个;

I can start a session with session_start() , set a few session variables, toss them around within functions, using them as globals, and at the end of script, I unset them, and destroy the session and as a result, I assume NO SESSION FILE IS CREATED, thus no overhead associated with creating the session text files is experienced. 我可以用session_start()开始一个会话,设置几个会话变量,在函数中抛出它们,用它们作为全局变量,在脚本结束时,我取消它们,并销毁会话,结果,我假设没有创建了会话文件,因此不会产生与创建会话文本文件相关的开销。

I would like to hear your insights in this. 我想听听你对此的见解。

My purpose is to explore the possibilities of using session variables strictly as a temporary place holder to pass global variables left and right - without dealing with file I/O. 我的目的是探索使用会话变量作为临时占位符来左右传递全局变量的可能性 - 而不处理文件I / O. In fact, I do not need those variables in the next page at all. 实际上,我根本不需要在下一页中使用这些变量。

In this case, could sessions prove faster than using globals within functions? 在这种情况下,会话证明比在函数内使用全局变量更快吗?

Tested and it gets created immediately on session_start . 经过测试,它会立即session_start上创建。 However, session_destroy removes it too. 但是, session_destroy也会将其删除。 Tested with: 经测试:

mkdir sess && cd sess
vim main.php

session_save_path(dirname(__FILE__));
session_start();
sleep(5);
session_destroy();

php main.php &
ls # main.php   sess_rm4bcun6ear943mf61mdads190
fg # wait for script to end
ls # main.php

There's the answer to your question. 你问题的答案就是答案。 Your idea of using _SESSION for global variable purposes is not a good one .. you might as well juse use $GLOBALS . 您将_SESSION用于全局变量目的的想法并不是一个好的...您也可以使用$GLOBALS No file IO there. 那里没有文件IO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 php会话何时确切到期? - When exactly does a php session expire? 无法在类文件中创建会话 - Could not get session created in class file 会话劫持在 PHP 中究竟是如何工作的? - How exactly does session hijacking work in PHP? 会话过期时对象是否被销毁? - Does object get destroyed when session expires? 这到底是什么意思? $ _SESSION ['sessionName'] [$ item ['rowName']] - What does this mean exactly? $_SESSION['sessionName '][$item['rowName ']] 这个“ $ this->会话-> userdata(” datiPreventivo“);”到CodeIgniter控制器类中到底是什么? - What exactly does this “$this -> session -> userdata(”datiPreventivo“);” into a CodeIgniter controller class? 什么时候在PHP中创建“符号” /“变量名称”? - When does a 'symbol' / 'variable name' get created in PHP? 当我在 jQuery 中运行 $.get 时,我收到“object Object”。 为什么? 究竟是什么意思? - When I run $.get in jQuery I receive "object Object". Why? What does mean exactly? 使用$ _GET时,$ _ SESSION变量不会持续到第一页之外 - $_SESSION variable does not persist beyond first page when using a $_GET 什么时候PHP 5.5+ Opcache确实根据revalidate_freq设置检查文件时间戳 - When exactly does PHP 5.5+ Opcache check file timestamp based on revalidate_freq setting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM