Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session_start() takes VERY LONG TIME

Mys site works very slowly (and I didn't have any idea about why). It is based on Zend Application, I used to make about tens of such sites, so I'm sure that MY code is OK.

I installed xdebugger on server, tried to profile it and guess what? php::session_start() took 48.675 seconds. Fourty Eight and a Half Seconds! It's unbelievable! What could be the reason of this? It's common operation, why could it execute SO long? How to fix such behaviour, which configs to edit? Searched over Google, but found no good answer (almost everywhere there's a question, but no answer). Thanks in before!

xdebugger profiling results

like image 356
ABTOMAT Avatar asked Dec 07 '12 22:12

ABTOMAT


People also ask

When should the session_start () function be used?

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie. When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers.

Do I need to use session_start on every page?

It must be on every page you intend to use. The variables contained in the session—such as username and favorite color—are set with $_SESSION, a global variable. In this example, the session_start function is positioned after a non-printing comment but before any HTML.

What is PHP session_start () and session_destroy () function?

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called. Note: You do not have to call session_destroy() from usual code.

How do I start and destroy a session?

session_start() will start session. session_destroy() will destroy session. For setting session data you could do this.


1 Answers

session_start (with sessions stored in files) is blocking in PHP, so this issue will appear if you try to start several server sessions for the same browser session (AJAX or multiple browser tabs/windows). Each session_start will wait until the other sessions have been closed.

See here: http://konrness.com/php5/how-to-prevent-blocking-php-requests/

Try changing from files to database storage of sessions.

like image 117
Christian Davén Avatar answered Oct 05 '22 19:10

Christian Davén