Booba__2012
Booba__2012

Reputation: 153

$_SESSION values lost after a header('Location: .........')

Here is my code :

$login      = $_SESSION['login'];
$statut     = $_SESSION['statut'];
$atelier    = $_SESSION['atelier'];
$connexion  = $_SESSION['connexion'];
session_destroy ();
$_SESSION['login']      = $login;
$_SESSION['statut']     = $statut;
$_SESSION['atelier']    = $atelier;
$_SESSION['connexion']  = true;
header('Location: http://10.13.48.60/no_serie.php');

Soo when I arrive here, the header send me to the correct page but I loose each time my login value like if it didn't save again my $_SESSION value. Am I doing something wrong ?

Or an another solution would be to put the variables in the link like :

header('Location: http://10.13.48.60/no_serie.php?login = '.$login.', statut = '.$statut.', ...');

But I prefer the $_SESSION solution.

ps : I start each of my page with session_start(); and I never touch to the $_SESSION value.

Thanks

Upvotes: 0

Views: 993

Answers (2)

Shaeldon
Shaeldon

Reputation: 883

You destroy the complete session but you dont (re)start it.

session_destroy();
session_start();

Upvotes: 1

Luke
Luke

Reputation: 4063

You called session_destroy() then set more sessions, so you need to make sure session_start() is called again before resetting the sessions. Or don't call session_destroy at all.

Like so:

$login      = $_SESSION['login'];
$statut     = $_SESSION['statut'];
$atelier    = $_SESSION['atelier'];
$connexion  = $_SESSION['connexion'];

// session_destroy (); - unnecessary

$_SESSION['login']      = $login;
$_SESSION['statut']     = $statut;
$_SESSION['atelier']    = $atelier;
$_SESSION['connexion']  = true;
header('Location: http://10.13.48.60/no_serie.php');

From PHP.net:

session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

http://php.net/manual/en/function.session-destroy.php

Upvotes: 5

Related Questions