0

I had a question where tried to get the session variable that defined in another page. But get error that the session had been started. when remove the session_start(), it say undefined variable. Tried to search for other relevant post but seems no clear solution given.. pls advise.. thanks..

session_start();
$CustID = $_SESSION['UserID'];
user2649074
  • 229
  • 1
  • 3
  • 8
  • possible duplicate of [Check if PHP session has already started](http://stackoverflow.com/questions/6249707/check-if-php-session-has-already-started) –  Aug 19 '13 at 05:01
  • the session is started , but when i do $id = $_SESSION['UserID']; it return error for undefined variable. the variable i defined in another page. how can i fix this.. pls help..thanks – user2649074 Aug 19 '13 at 06:31

2 Answers2

0

Either you are including the file multiple times on a page; or you are including it on a page that has its' own session_start() statement; or your php.ini (possibly .htaccess) is automatically starting a session before you do - http://www.php.net/manual/en/ref.session.php#ini.session.auto-start

You can use the following logic to only start a session if it has not already been started -

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

try having a core folder and a init.php where you start sessions there, session_start() and include this everywhere you go, this can also have database connection and all you want to be cool everywhere

then play with sessions with out this kind of errors

  • thanks for reply. my problem is if no start, then start the session, but when i start the session, it return me error that session already started.. – user2649074 Aug 19 '13 at 06:21
  • yes, i include another php page where the session had been started there.. but why this page couldn't detect it and get the id that defined in another page? – user2649074 Aug 19 '13 at 06:23
  • @user2649074 if you think another session has ended because you are destroying it you might have the mistake of not adding `session_start()`; before `session_destroy();` before destroying a session you must start sessions in that file – Young Student Aug 19 '13 at 06:34
0

The session might have started multiple times in the same page. Check all your included files and the current file. Make sure session is not started multiple times.

Better thing to do is, always start session at the top of the page.

Leo T Abraham
  • 2,407
  • 4
  • 28
  • 54