In PHP, Is There Any Harm in Running Session_Start() Multiple Times

In PHP, is there any harm in running session_start() multiple times?

From the docs:

As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

So no, it won't "cause harm", but it'll throw an error. And the fact that it's happening is probably an indicator that you're doing something incorrectly, and might need to re-think how your code is laid out.

Using session_start twice

PHP doesn't support multiple simultaneous sessions. Calling session_start() a second time in a request doesn't do anything unless the existing session was destroyed (via session_destroy()).

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.

http://php.net/session-start

PHP session handling when the same client requests the same script multiple times at once

Session data, be default, is not saved until the request terminates. So your increment is not saved while sleeping. If you want to save the session prematurely checkout session_write_close()

PHP: session_start() called 'simultaneously' in multiple tabs creates multiple sessions

The best solution I could come up with was to move the session "creation" (i.e., setting session variables) into the ajax.php file, executed only after a user has successfully sent their uname/pwd and so they are about to be redirected to a new page anyway (i.e., welcome.php). This means that login.php cannot be guaranteed to have access to any session variables set by ajax.php whatsoever, so it's just a dumb page that relies solely on its ajax calls to know what's going on. As it turns out, this isn't such a hassle after all.

Problem with function session_start() (works slowly)

The file containing the session's informations is locked during the time PHP serves a request.

So, if you have one PHP script (using a session) that's currently being executed, and the same user sends another request, the second request will wait until the first is finished.



Related Topics



Leave a reply



Submit