douzuqin3467 2012-09-20 14:17
浏览 51
已采纳

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

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(). But is it? Could it be all the way at the end when the php script ends?

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. For example, if you got something like $_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'].

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. 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.

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. 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?

  • 写回答

1条回答 默认 最新

  • draxu26480 2012-09-20 14:27
    关注

    Tested and it gets created immediately on session_start. However, session_destroy removes it too. 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. No file IO there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?