PHP Sessions That Have Already Been Started

PHP sessions that have already been started

Try

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

PHP session has already been started

You should check the session status with session_status() function to avoid repeated session start. For example:

if (session_status() == PHP_SESSION_NONE) {
session_start();
}

Check if PHP session has already started

Recommended way for versions of PHP >= 5.4.0 , PHP 7, PHP 8

if (session_status() === PHP_SESSION_NONE) {
session_start();
}

Reference: http://www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0

if(session_id() == '') {
session_start();
}

PHP: A session had already been started - ignoring session_start() comes only in windows

You said that you starts session with a check:

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

The fact is the $_SESSION always exists and if you aren't put something in it then it will be always empty, so the statment will return always true.

PHP sessions and session_start()

That's may be because that you have been redirected to another site during the process. And while you return from Paypal to your website, session_start() generated a new session id which your previously stored session variables are not linked to.

And when you removed session_start() (I don't think session should work without this on top), it used the old session id and never got regenerated. Hence, old session data are back!

This is just my assumption.

Load session_start() only if session does not exist?

isset is generally the proper way to check if predefined variables are currently defined:

If you are using a version of php prior to 5.4,

you can usually get away with and its tempting just doing the code below, but be aware that if for some reason sessions are disabled, session_start() will still be called which could end up leading to errors and needless debugging since the $_SESSION array isn't allowed to be created.

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

if you want to account for checking to see if sessions are disabled for some reason, you can supress the error message, set a test session variable, and then verify it was set. If its not, you can code in a reaction for disabled sessions. You'll want to do all this at or near the start of your script. So as an example(works differently depending on error handling setting):

if(!isset($_SESSION)){session_start();}
$_SESSION['valid'] = 'valid';
if($_SESSION['valid'] != 'valid')
{
//handle disabled sessions
}

However, if you are using php version 5.4 or greater,

You can use the session_status() function, a better option as it accounts for sessions being disabled and checks if a session already exists.

if (session_status() === PHP_SESSION_NONE){session_start();}

NOTE that PHP_SESSION_NONE is a constant set by PHP and therefore does not need to be wrapped in quotes. It evaluates to integer 1, but as its a constant that was specifically set to eliminate the need for magic numbers, best to test against the constant.

session_start gives notice that its already started.

If you can't make sure your session is always started, you have to check if its started:

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


Related Topics



Leave a reply



Submit