Namit
Namit

Reputation: 1322

session_start gives notice that its already started.

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

Upvotes: 2

Views: 6436

Answers (4)

Dmitry Dubovitsky
Dmitry Dubovitsky

Reputation: 2236

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

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

Upvotes: 1

Rufinus
Rufinus

Reputation: 30753

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

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

Upvotes: 8

Richard Sparrow
Richard Sparrow

Reputation: 101

Ensure you have session_start() once after connecting to your database (if you have one) and before outputting anything at all to the browser. Make sure there is no white space in front of session_start() to minimize accidental output.

Upvotes: 0

blaff
blaff

Reputation: 304

You only have to start session_start() once.

Upvotes: 0

Related Questions