Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

session_start gives notice that its already started.

Tags:

php

session

I have a php file which starts a session, this is followed by some html. Later i include another file using include "some_file.php". The some_file.php has php and mysql interaction which is used to populate a table. Now i am starting a session in some_file.php as well, as i need to access the session variables. However i thought that the by using include i won't have to start a session again. But on doing this i get an error saying undefined session variable in some_file.php, and by using it, i get a notice saying Notice: A session had already been started - ignoring session_start() in some_file.php/some_line

like image 933
Namit Avatar asked Dec 29 '25 20:12

Namit


2 Answers

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

if(!isset($_SESSION)){
 session_start();
}
like image 57
Rufinus Avatar answered Jan 01 '26 11:01

Rufinus


if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

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

like image 41
Dmitry Dubovitsky Avatar answered Jan 01 '26 13:01

Dmitry Dubovitsky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!