BruceyBandit
BruceyBandit

Reputation: 4334

$_SESSION's are not being retrieved

I have sessions on 3 individual pages:

teacherlogin.php (page 1)

while($row = mysql_fetch_array($result))
  {   
      session_start(); 
$_SESSION['teacherforename'] = $row['TeacherForename'];
$_SESSION['teachersurname'] = $row['TeacherSurname'];

  }

create_session.php (page 2)

     while ($row = mysql_fetch_array($result)) { 
            $dataArray[$row['CourseId']]['CourseName'] = $row['CourseName']; 
            $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName']; 

$_SESSION['idcourse'] = $row['CourseId'];
$_SESSION['namecourse'] = $row['CourseName'];

    }

QandATable.php (page 3)

session_start();
<p>{$_SESSION['courseid']}  {$_SESSION['namecourse']}</p>
<p>{$_SESSION['teacherforename']} {$_SESSION['teachersurname']}</p>

Now what happens is that page 3 is able to retrieve the $_SESSION's from page 1, but when I try and get page 3 to retrieve $_SESSIONS from page 2, it comes with an undefined index notice for both page 2 $_SESSIONS. Why is this?

I tried including session_start() for page 2 but it comes back with notice stating session has already started so ignoring session_start().

Upvotes: 0

Views: 71

Answers (1)

dotancohen
dotancohen

Reputation: 31491

You are starting a new session for each result. Move the session_start() function to be before the while loop.

Upvotes: 3

Related Questions