I have a PHP file “included.php” which is included in another PHP file.
I like to confirm whether “included.php” read “session_start()” or not read.
The following code doesn’t work correctly but I hope it show what I want.
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
if ( isset(session_start()) ) {
echo 'Yes session is already started, So, no need to session starting';
} else {
session_start()
echo "No session is started, therefore, session starting here';
}
I tried the script and recieved a different error message:
Fatal error : Cannot use isset() on the result of an expression (you can use “null !== expression” instead) in /var/www/0-tests.php on line 7
The suggestions works as expected although I altered their boolean test because I prefer to test for positives rather than NOT positive - seems I have to flip my mind twice to understand what is happening:
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
// if ( isset( session_start() ) )
if ( NULL === session_start() )
{
// Following line not required - TO CHECK WHEN ON DESKTOP
// session_start();
echo 'No session is started, therefore, session starting here';
} else {
echo 'Yes session is already started, So, no need to
session starting';
}
Although the script works OK I prefer to use @Andres_Vaquero solution which also saves repeating a function call.
Remmed second session_start() because session already started.
I think you guys are making it more difficult than it should be. There are default functions for this.
In PHP 5.4 and lower, you’d just do
if(session_id() == '') {
// Initiate the session start here
}
And for PHP 5.5 and above, you’d just do
if(session_status() == PHP_SESSION_NONE) {
// Initiate the session start here
}
And if the session was already started, you really don’t need to say it’s already started. Just don’t create another instance of session_start() and you won’t break your application.
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
// Both these suppress errors and start the session OK
// NULL !== session_start();
@session_start();
echo '<pre>'; print_r($_SESSION);
<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');
// Both these suppress errors and start the session OK
// @Andres_Vaquero NULL !== session_start();
@session_start();
echo '<pre>'; print_r($_SESSION);
Well, there are instances when you may want to make sure a session hasn’t been started already. Say for example you’ve already created a session in your application. You run composer and grab down a library that you really like. This library also has a session_start line in there. Now you’ve broke your code because the 3rd party library wants to create a session as well.
I actually know a few libraries that do this. The only way is to double check if the session is already started. If it isn’t, just allow the library to create the session instead so you don’t get 2 instances of a session_start where it may blow up your application.
I wondered if things had changed since I first learnt PHP, so I googled PHP session_start.
From the PHP manual
“(PHP 4, PHP 5, PHP 7)
session_start — Start new or resume existing session”
So no need to try and detect an existing session.
And it is more concise.
And it adheres to the KISS principle.