Michiel
Michiel

Reputation: 8083

$_SESSION doesn't take data over different pages

For some reason I don't manage to take some date from one page to another using the $_SESSION-variable.

On my index.php-page is a form where users can login.
I start with (where session_start() is in the inc_admin.php-file):

require_once '../includes/php/inc_admin.php';

if ($_GET['page'] == 'login') {
    $action = loginUser($_DB, $_POST['firstname'], $_POST['password']);
}

if(isset($_SESSION["user"])) {
    $action .= header('Location: ../admin/index.php');
}

echo $action;

The loginUser-function does set (among other) the $_SESSION:

$user = Leaders::getLogin($_DB, $firstname, $password);
if(!empty($user)) {
    $_SESSION["user"] = $user;
}

And as t should, when the user submits the form with correct data, he will be redirected to the ../admin/index.php-page. However, if I take a var_dump($_SESSION), the page tells me this variable is NULL.

What more do I need to do to make sure data is transferred over pages using $_SESSION?

Upvotes: 0

Views: 100

Answers (3)

Raheel Hasan
Raheel Hasan

Reputation: 6023

Call "session_start();" before any output on the page (even before any Header() is sent). Best is to place it at the very top of the page.

Upvotes: 0

Rens Groenveld
Rens Groenveld

Reputation: 982

Make sure you call session_start() somewhere everytime you reload a page, or else the server won't get the session details.

Upvotes: 1

Dustin Moorman
Dustin Moorman

Reputation: 104

Each page needs to begin with

session_start();

it resumes the previously created session

Upvotes: 2

Related Questions