How to lock session_start in PHP?

Initially, I just want to verify that session_start is blocking in the session. So, I am creating a PHP file as shown below. In principle, if the page view is flat, the page sleeps for 10 seconds; if the page view is odd, itโ€™s not. And session_start is used to get the pageview in $ _ SESSION .

I tried to open the page in two tabs of the same browser. Unsurprisingly, the first tab takes 10 seconds, since I explicitly let her sleep. The second tab will not sleep, but it should be blocked by sessiont_start . This works as expected.

To my surprise, the output of the second page shows that session_start is running out of time. In fact, the entire page does not seem to take time to load. But in the browser, the page takes 10 seconds.

obtained lock Cost time: 0.00016689300537109 Start 1269739162.1997 End 1269739162.1998 allover time elpased : 0.00032305717468262 The page views: 101 

Does PHP output session_start from a PHP page and execute it before other PHP instructions?

This is the code.

 <?php function float_time() { list($usec, $sec) = explode(' ', microtime()); return (float)$sec + (float)$usec; } $allover_start_time = float_time(); $start_time = float_time(); session_start(); echo "obtained lock<br/>"; $end_time = float_time(); $elapsed_time = $end_time - $start_time; echo "Cost time: $elapsed_time <br>"; echo "Start $start_time<br/>"; echo "End $end_time<br/>"; ob_flush(); flush(); if (isset($_SESSION['views'])) { $_SESSION['views'] += 1; } else { $_SESSION['views'] = 0; } if ($_SESSION['views'] % 2 == 0) { echo "sleep 10 seconds<br/>"; sleep(10); } $allover_end_time = float_time(); echo "allover time elpased : " . ($allover_end_time - $allover_start_time) . "<br/>"; echo "The page views: " . $_SESSION['views']; ?> 
+7
php session
source share
3 answers

This seems like a firefox related issue. If you request the same URL in two tabs / windows, the second request expects the completion of the first request (it may also be an addon that blocks the second request, did not check this).
Take for example

 <?php // test.php $start = microtime(true); echo "<pre>start: $start</pre>"; sleep(5); $end = microtime(true); echo '<pre>', $start, "\n", $end, "\n", $end-$start, '</pre>'; 

I called it twice, and the result was

 start: 1269742677.6094 1269742677.6094 1269742682.609 4.9995958805084 

and

 start: 1269742682.6563 1269742682.6563 1269742687.6557 4.9994258880615 

Note that there is already a 5 second gap between the start time.

When calling http://localhost/test.php and http://localhost/test.php?a=b instead of the same URL, this does not happen twice.
Both IE8 and Chrome do not show this behavior.

+4
source share

Yes, this may be because session_start () blocks other requests in the same session (file-based). I was able to check the problem in Firefox (4.x) and Chrome (10.x) in Windows XP / PHP 5.2 using the default session handler (file). I am not sure if this problem is reproduced for non-file session handlers.

 obtained lock **Cost time: 9.90100598335** Start 1303227658.67 End 1303227668.57 sleep 10 seconds allover time elpased : 19.9027831554 The page views: 4 

This is a very interesting issue, and the Firefox tab lock described in the answer above would overshadow it from being detected.

http://www.php.net/manual/en/function.session-start.php#101452

+1
source share

Since php has no container. How to make two calls in one session? Who is doing this? What do these two processes say? Is the PHP module always active and only spawns threads after checking the session? In this case, the PHP module really behaves like a container, which in this case provides a session management service in this area.

0
source share

All Articles