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
Setting a Cookie in an Ajax Request
Setting Variables on Constructor VS on the Class Definition
Symfony: How to Refresh the Authenticated User from the Database
Regular Expression for Checking Website Url
Pass the Chosen Product Variations Data into Contact Form 7 Enquiry Form
PHP Strtotime Returns a 1970 Date When Date Column Is Null
Phpseclib - How to Connect Using Username, Key and Password (Not a Key Passphrase)
How to Pass Variables Between PHP Scripts
How to Obtain the Last Word of a String
Escaping SQL Queries in Codeigniter
Cannot Initialize Mbstring with PHP 7
Incrementing Numbers Starting from 0000 in PHP
Swap Two Words in a String PHP
PHP Twitter API - How to Pull in Multiple Users Tweets
Passing Variable from Controller to View in Codeigniter
Pass Form Data to Another Page with PHP
Discount for Certain Category Based on Total Number of Products