PHP sessions already started

In my PHP code, if the session is already running and I try to start a new one, I get the following notification:

Note: the session is already running - ignoring session_start ()

How can i avoid this?

+71
php session
May 18 '12 at 8:06
source share
11 answers

Try

<?php if(!isset($_SESSION)) { session_start(); } ?> 
+217
May 18 '12 at 8:09 a.m.
source share
— -

If you want a new one then session_destroy() before starting. To check if its set is installed before running it, call session_status() :

 $status = session_status(); if($status == PHP_SESSION_NONE){ //There is no active session session_start(); }else if($status == PHP_SESSION_DISABLED){ //Sessions are not available }else if($status == PHP_SESSION_ACTIVE){ //Destroy current and start new one session_destroy(); session_start(); } 

I would not check the global $_SESSION instead of calling the session_status() method, since PHP explicitly implemented this function:

Open session status with the new session_status function. This is for (PHP> = 5.4.0)

+28
May 18 '12 at 8:09 a.m.
source share

Only if you want to destroy the previous session:

 <?php if(!isset($_SESSION)) { session_start(); } else { session_destroy(); session_start(); } ?> 

or you can use

 unset($_SESSION['variable_session _data']) 

to destroy a specific session variable.

+4
May 18 '12 at 8:52
source share

Yes, you can determine if a session is running by checking isset($_SESSION) . However, the best answer is to simply not call session_start() more than once.

It should be called very early in your script, perhaps even on the first line, and then not called again.

If you have this in more than one place in your code, you are asking for this error. Reduce it so that it is only in one place and can only be called once.

+2
May 18 '12 at 23:32
source share

You should already name the beginning of the session, maybe again called via include?

 if( ! $_SESSION) { session_start(); } 
+1
May 18 '12 at 8:09
source share
 <?php if ( session_id() != "" ) { session_start(); } 

Basically, you need to check if the session was started before creating another; ... more reading.

On the other hand, you decided to destroy the existing session before creating another using session_destroy() .

0
May 18 '12 at 8:12
source share

try it

 if(!isset($_SESSION)){ session_start(); } 

I suggest you use ob_start (); before starting any sesson varriable, this should order a browser buffer.

edit: Added optional) after $ _SESSION

0
Sep 25 '14 at 9:10
source share

None of the above apply without calling session_start () in all php files that depend on the $ Session variables that they will not be included. The notification is so annoying and quickly populates Error_log. The only solution I can find is that ...

  error_reporting(E_ALL ^ E_NOTICE); session_start(); 

Bad fix, but it works.

0
Jun 16 '17 at 8:37
source share

I ran into this problem trying to fix the $ _SESSION lock behavior.

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

The session file remains locked until the script completes or the session is closed manually.

Thus, by default, the page should open the session in read-only mode. But as soon as it opens read-only, it should be closed and reopened in write mode.

 const SESSION_DEFAULT_COOKIE_LIFETIME = 86400; /** * Open _SESSION read-only */ function OpenSessionReadOnly() { session_start([ 'cookie_lifetime' => SESSION_DEFAULT_COOKIE_LIFETIME, 'read_and_close' => true, // READ ACCESS FAST ]); // $_SESSION is now defined. Call WriteSessionValues() to write out values } /** * _SESSION is read-only by default. Call this function to save a new value * call this function like `WriteSessionValues(["username"=>$login_user]);` * to set $_SESSION["username"] * * @param array $values_assoc_array */ function WriteSessionValues($values_assoc_array) { // this is required to close the read-only session and // not get a warning on the next line. session_abort(); // now open the session with write access session_start([ 'cookie_lifetime' => SESSION_DEFAULT_COOKIE_LIFETIME ]); foreach ($values_assoc_array as $key => $value) { $_SESSION[ $key ] = $value; } session_write_close(); // Write session data and end session OpenSessionReadOnly(); // now reopen the session in read-only mode. } OpenSessionReadOnly(); // start the session for this page 

Then, when you move on to writing some value:

 WriteSessionValues(["username"=>$login_user]); 
0
Jul 07 '17 at 4:40
source share

This would be more efficient:

 @session_start(); 

Avoid the error handler on the screen.

Best

0
Aug 23 '17 at 22:48
source share

replace session_start (); before

 if(!isset($a)) { $a = False; if($a == TRUE) { session_start(); $a=TRUE; } } 
-3
Aug 17 '16 at 5:08
source share



All Articles