Reputation: 217
I test this script in chrome.
<?php
session_start();
if(isset($_SESSION['ddd']) && $_SESSION['ddd']) {
exit("SESSION is set");
} else {
$_SESSION['ddd'] = true;
session_write_close();
//session_commit() is also not work
sleep(5);
exit("SESSION is working");
}
I open the PHP file in Chrome, and it echoes "SESSION is working" after 5 seconds.
If I open the file in a new tab before 5 seconds have elapsed, it echoes "SESSION is working", instead of "SESSION is set".
To confirm, if I open the file in a new tab after 5 seconds have elapsed, it does echo "SESSION is set".
Why? Does session_write_close
not commit session data immediately? If it is so, should I want to commit the session immediately, how might I do?
Upvotes: 2
Views: 1632
Reputation: 584
I think you did something wrong: Never open multiple tabs before you have the PHPSESSID cookie, or you will get the unexpected result.
The first time you run the script, you don't have the PHPSESSID cookie yet. And you run the script again before the first request is done, you will get a NEW session (different session_id), so you won't get the session data of the first request.
Therefore, you should ensure that you have the PHPSESSID in your browser cookies.
Change the code to
session_start();
var_dump($_SESSION);
$_SESSION['val'] = isset($_SESSION['val']) ? ++$_SESSION['val'] : 0;
session_commit();
sleep(5);
exit;
First, run the script once, waiting until it's done.
Then open second tab and third tab in 5 seconds.
Now you will get different values.
1st tab: array(0) { }
2nd tab: array(1) { ["val"]=> int(1) }
3rd tab: array(1) { ["val"]=> int(2) }
It shows that the 3rd tab got the correct value after 2nd tab session_commit.
Upvotes: 3
Reputation: 217
I am sorry that maybe I have found the reason! 1、It is the reason about the sleep function! if using the sleep,the script file will be blocked. When a new request about this php file will be excute after 5 seconds later.If use another php file with same code will be better. 2、Someone said that a new session will be not fast enough.Just look at this link,the third tip! enter link description here Now,I should create the session first. OK,I create three php files.
fisrt - index.php to set the session;
<?php
session_start();
$_SESSION['ddd']=1;
session_commit();
?>
second -index2.php
<?php
session_start();
if(isset($_SESSION['ddd']) && $_SESSION['ddd']==2)
{
echo("SESSION is set");
}
else {
$_SESSION['ddd'] = 2;
session_commit();
echo "SESSION is working";
sleep(5);
}
?>
third index3.php
<?php
session_start();
if(isset($_SESSION['ddd']) && $_SESSION['ddd']==2)
{
echo("SESSION is set");
}
else {
$_SESSION['ddd'] = 2;
session_commit();
echo "SESSION is working";
for ($i=0;$i<10000000;$i++)
{
if(file_exists('sdsd'));
}
}
?>
It is very interesting.
fisrt lab: open the index.php first. then open the index2.php in two tabs,you will see, the second tab of index2.php echo 'SESSION is set' but,it will return after 5 seconds,yeah,sleep function block it,but the session has written.Good news.
second lab: open the index.php first. then open the index2.php,index3.php in two tabs,you will see, the second tab echo 'SESSION is set' very fast!
Someone else if interesting with this can give me more help!
Upvotes: 0