SlideShare une entreprise Scribd logo
PHP : Sessions & cookies
1. Les Superglobales
Définition
Les Superglobales sont des variables internes qui sont toujours
disponibles, quel que soit le contexte
Source : http://php.net
Les Superglobales
$GLOBALS
-
$_SERVER
-
$_GET
-
$_POST
-
$_FILES
-
$_COOKIE
-
$_SESSION
-
$_REQUEST
-
$_ENV
Maman comment afficher le contenu d’une superglobale ?
<?php
var_dump($_POST);
?>
2. Session
Définition
Sessions allow the PHP script to store data on the web server
that can be later used, even between requests to different PHP
pages. Sessions end when the user closes the browser, or when
the web server deletes the session information, or when the
programmer explicitly destroys the session.
Source : http://en.wikibooks.org
Utilisation (1)
At the top of each PHP script that will be part of the
current session there must be the function
session_start(). It must be before the first output (echo
or others) or it will result in an error "Headers already
sent out".
Source : http://en.wikibooks.org
Utilisation (2)
<?php
session_start();
$_SESSION['prenom'] = "Jean";
$_SESSION['nom'] = "Michel";
?>
<?php
session_start();
echo $_SESSION['prenom'];
// Jean
echo $_SESSION['nom'];
// Michel
?>
page_1.php page_2.php
3. Cookies
Définition
Les cookies sont un mécanisme d'enregistrement
d'informations sur le client, et de lecture de ces
informations. Ce système permet d'identifier et de
suivre les visiteurs.
Source : http://php.net
Utilisation (1)
At the top of each PHP script that will be part of the
current session there must be the function
session_start(). It must be before the first output (echo
or others) or it will result in an error "Headers already
sent out".
Source : http://en.wikibooks.org
Utilisation (2)
page_1.php page_2.php
<?php
echo $_COOKIE["TestCookie"];
/* Toto avant h +1 heure */
?>
<?php
setcookie(
"TestCookie",
'toto',
time()+3600); /* 1 heure */
?>
4. Mise en pratique
Exercice : En route pour l’école !
Créer la page « fin de quizz » avec l’affichage du score global
de l’utilisateur. Ecrans disponibles ici :
https://www.dropbox.com/sh/34xttwuv09xx07e/AABUFOxVwHRYHeUQQCBPmp9Ua?dl=0
Après 10 questions, l’utilisateur arrive sur la page « fin de
quizz ».
La page « fin de quizz » indique si le résultat de l’utilisateur
est son meilleur score de la journée.
Merci pour votre attention.
Crédits
Perfect Chocolate Chip Cookies - Kimberly Vardeman
http://www.flickr.com/photos/kimberlykv/4643536339/

Contenu connexe

ZIP
Draft - Developper Sur Elgg
PDF
Django compressor
PPTX
Retours Devoxx France 2016
PDF
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
PPTX
Workshop Spring 3 - Tests et techniques avancées du conteneur Spring
PDF
Installation apache mandriva
PDF
Le blog technique de laurent besson(1)
PDF
#3 html in the real world
Draft - Developper Sur Elgg
Django compressor
Retours Devoxx France 2016
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
Workshop Spring 3 - Tests et techniques avancées du conteneur Spring
Installation apache mandriva
Le blog technique de laurent besson(1)
#3 html in the real world

En vedette (20)

PDF
Javascript #9 : barbarian quest
PDF
Javascript #8 : événements
PDF
Une introduction à Javascript
PDF
Wordpress #1 : introduction
PDF
Javascript #11: Space invader
PDF
PHP #6 : mysql
PDF
Gestion de projet #3 : besoin client
PDF
Javascript #2.2 : jQuery
PDF
Javascript #4.2 : fonctions for pgm
PDF
WebApp #2 : responsive design
PDF
PHP & MYSQL #5 : fonctions
PDF
PHP #7 : guess who?
PDF
Wordpress #3 : content strategie
PDF
WebApp #4 : Consuming REST APIs
PDF
Les modèles économiques du web
PDF
Architecture logicielle #5 : hipsto framework
PDF
Architecture logicielle #4 : mvc
PDF
Javascript #5.1 : tp1 zombies!
PDF
Projet timezone
PDF
Startup & entrepreneuriat #2.1: disrupt me
Javascript #9 : barbarian quest
Javascript #8 : événements
Une introduction à Javascript
Wordpress #1 : introduction
Javascript #11: Space invader
PHP #6 : mysql
Gestion de projet #3 : besoin client
Javascript #2.2 : jQuery
Javascript #4.2 : fonctions for pgm
WebApp #2 : responsive design
PHP & MYSQL #5 : fonctions
PHP #7 : guess who?
Wordpress #3 : content strategie
WebApp #4 : Consuming REST APIs
Les modèles économiques du web
Architecture logicielle #5 : hipsto framework
Architecture logicielle #4 : mvc
Javascript #5.1 : tp1 zombies!
Projet timezone
Startup & entrepreneuriat #2.1: disrupt me
Publicité

Similaire à PHP #4 : sessions & cookies (20)

PPTX
pro_Langage_PHP_LES_FONCTIONALITES_AVANCEES__L2_ DAWN & ASRS.pptx
PPTX
PHP (Partie II) Par Mahdi Ben Alaya
PDF
Chap2
PPTX
Initiation au php
PPTX
Initiation au php
PPT
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
PPT
PHP_partie_.ppt,php_cours_mathematiques_informatiques
PDF
PHP_Essentials.pdfPHP_Essentials.pdfPHP_Essentials.pdf
PDF
Alphorm.com Formation PHP 8 : Gestion des Sessions
PDF
Cours php & Mysql - 3éme partie
PDF
Developpement web dynamique_Base de donnees.pdf
PPT
Introduction à PHP
PPTX
Loucif Redha développement d'applications web - php - my sql
PDF
Php seance1
PDF
PHP mysql Xml.pdf
PDF
PROGRAMMES FASCICULE DE PHP IDA2 (1).pdf
PDF
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
DOC
PHP mysql Xml.doc
PDF
Résumé Complet : Les Fondamentaux du PHP et Intégration avec MySQL.pdf
PPT
cours-gratuit.com--CoursPhp-id1055.ppt
pro_Langage_PHP_LES_FONCTIONALITES_AVANCEES__L2_ DAWN & ASRS.pptx
PHP (Partie II) Par Mahdi Ben Alaya
Chap2
Initiation au php
Initiation au php
Introduction au langage PHP (2éme partie) élaborée par Marouan OMEZZINE
PHP_partie_.ppt,php_cours_mathematiques_informatiques
PHP_Essentials.pdfPHP_Essentials.pdfPHP_Essentials.pdf
Alphorm.com Formation PHP 8 : Gestion des Sessions
Cours php & Mysql - 3éme partie
Developpement web dynamique_Base de donnees.pdf
Introduction à PHP
Loucif Redha développement d'applications web - php - my sql
Php seance1
PHP mysql Xml.pdf
PROGRAMMES FASCICULE DE PHP IDA2 (1).pdf
Cours3-PHPfgdwfwdffhddfbwdfwdfwdfwdfwfw.pdf
PHP mysql Xml.doc
Résumé Complet : Les Fondamentaux du PHP et Intégration avec MySQL.pdf
cours-gratuit.com--CoursPhp-id1055.ppt
Publicité

Plus de Jean Michel (17)

PDF
Startup #7 : how to get customers
PDF
HTML & CSS #10 : Bootstrap
PDF
Javascript #10 : canvas
PDF
Architecture logicielle #3 : object oriented design
PDF
Architecture logicielle #2 : TP timezone
PDF
Architecture logicielle #1 : introduction
PDF
Wordpress #2 : customisation
PDF
PHP #3 : tableaux & formulaires
PDF
PHP #2 : variables, conditions & boucles
PDF
PHP #1 : introduction
PDF
Dev Web 101 #2 : development for dummies
PDF
Startup #5 : pitch
PDF
WebApp #3 : API
PDF
Gestion de projet #4 : spécification
PDF
Projet timezone
PDF
WebApp #1 : introduction
PDF
Javascript #7 : manipuler le dom
Startup #7 : how to get customers
HTML & CSS #10 : Bootstrap
Javascript #10 : canvas
Architecture logicielle #3 : object oriented design
Architecture logicielle #2 : TP timezone
Architecture logicielle #1 : introduction
Wordpress #2 : customisation
PHP #3 : tableaux & formulaires
PHP #2 : variables, conditions & boucles
PHP #1 : introduction
Dev Web 101 #2 : development for dummies
Startup #5 : pitch
WebApp #3 : API
Gestion de projet #4 : spécification
Projet timezone
WebApp #1 : introduction
Javascript #7 : manipuler le dom

PHP #4 : sessions & cookies

  • 1. PHP : Sessions & cookies
  • 3. Définition Les Superglobales sont des variables internes qui sont toujours disponibles, quel que soit le contexte Source : http://php.net
  • 5. Maman comment afficher le contenu d’une superglobale ? <?php var_dump($_POST); ?>
  • 7. Définition Sessions allow the PHP script to store data on the web server that can be later used, even between requests to different PHP pages. Sessions end when the user closes the browser, or when the web server deletes the session information, or when the programmer explicitly destroys the session. Source : http://en.wikibooks.org
  • 8. Utilisation (1) At the top of each PHP script that will be part of the current session there must be the function session_start(). It must be before the first output (echo or others) or it will result in an error "Headers already sent out". Source : http://en.wikibooks.org
  • 9. Utilisation (2) <?php session_start(); $_SESSION['prenom'] = "Jean"; $_SESSION['nom'] = "Michel"; ?> <?php session_start(); echo $_SESSION['prenom']; // Jean echo $_SESSION['nom']; // Michel ?> page_1.php page_2.php
  • 11. Définition Les cookies sont un mécanisme d'enregistrement d'informations sur le client, et de lecture de ces informations. Ce système permet d'identifier et de suivre les visiteurs. Source : http://php.net
  • 12. Utilisation (1) At the top of each PHP script that will be part of the current session there must be the function session_start(). It must be before the first output (echo or others) or it will result in an error "Headers already sent out". Source : http://en.wikibooks.org
  • 13. Utilisation (2) page_1.php page_2.php <?php echo $_COOKIE["TestCookie"]; /* Toto avant h +1 heure */ ?> <?php setcookie( "TestCookie", 'toto', time()+3600); /* 1 heure */ ?>
  • 14. 4. Mise en pratique
  • 15. Exercice : En route pour l’école ! Créer la page « fin de quizz » avec l’affichage du score global de l’utilisateur. Ecrans disponibles ici : https://www.dropbox.com/sh/34xttwuv09xx07e/AABUFOxVwHRYHeUQQCBPmp9Ua?dl=0 Après 10 questions, l’utilisateur arrive sur la page « fin de quizz ». La page « fin de quizz » indique si le résultat de l’utilisateur est son meilleur score de la journée.
  • 16. Merci pour votre attention.
  • 17. Crédits Perfect Chocolate Chip Cookies - Kimberly Vardeman http://www.flickr.com/photos/kimberlykv/4643536339/