Afin de me familiariser avec l'utilisation de sessions j'ai créé un fichier page1.php et un fichier page2.php.
Dans le fichier page1.php, définition d’une session et affichage de celle-ci comme suivant :
page1.php
<?php
header('Content-type:text/html; charset=UTF-8'); // encodage UTF-8
session_start();
/*
if( session_id()=='' ){ session_start(); $message = 'session fonctionne'; }
else
{ $message = 'session ne fonctionne pas'; }
*/
// echo phpinfo(); // Vérification du fichier ini
//error_reporting(E_ALL); // en TEST !!
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- <meta http-equiv="content-type" content="text/html; charset=utf-8" /> -->
<meta charset="utf-8">
<meta name="author" content="" />
<meta name="copyright" content="Le contenu de ce site est protégé par les droits d'auteur." />
<meta name="description" content="Description complete"> <meta name="keywords" content="" />
<title>CESSION page 1</title>
</head>
<body>
<?php
echo 'Page numéro 1 ' . '</br>';
$_SESSION['favcolor'] = 'vert';
echo 'Couleur : ' . $_SESSION['favcolor'];
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
</body>
</html>
page2.php<?php
header('Content-type:text/html; charset=UTF-8'); // encodage UTF-8
session_start()
//error_reporting(E_ALL); // en TEST !!
?>
<!DOCTYPE HTML>
<html>
<head>
<!-- <meta http-equiv="content-type" content="text/html; charset=utf-8" /> -->
<meta charset="utf-8">
<meta name="author" content="" />
<meta name="copyright" content="Le contenu de ce site est protégé par les droits d'auteur." />
<meta name="description" content="Description complete"> <meta name="keywords" content="" />
<title>CESSION page 2</title>
</head>
<body>
<?php
echo 'Page numéro 2 ' . '</br>';
if(isset($_SESSION['favcolor'])) // si la variable de session est définie
{
//echo 'Couleur : ' . $_SESSION['favcolor'];
echo 'Couleur : ', htmlspecialchars($_SESSION['favcolor'], ENT_QUOTES, 'UTF-8');
}
else
{
echo '$_SESSION favcolor non indexé';
// header('Location: page1.php'); // si la variable de session favcolor n'est pas définie, on renvoie sur la page1.php (placer la condition en début de page)
}
/*
foreach($_SESSION as $cle => $_SESSION['favcolor'])
{
echo " <li><strong>".ucfirst($cle)." : </strong><em>". "Couleur : " . $_SESSION['favcolor']."</em></li>\n";
}
*/
echo '<br /><a href="page1.php?' . SID . '">page 1</a>';
?>
</body>
</html>
Le problème $_SESSION['favcolor'] dans la page2.php ne s’affiche pas en ligne mais fonctionne en local .$_SESSION['favcolor'] n'est pas reconnu (non indexé) dans la page2.php.
Mes configurations :
- Poste de travail : Windows 10
- PHP 7
- Création des pages PHP 1 et 2 en UFT8 sous notepad++
- Hébergeur 1 & 1
Cordialement.