SlideShare a Scribd company logo
2
Most read
9
Most read
11
Most read
sessions and cookies in PHP
Sessions in php
 Session variables used to storing user information to be used across multiple
pages
 e.g. username, password, ..etc
 By default, session variables last until the user closes the browser.
Example for Session
Php session_start() function
 A session is started with the session_start() function
 Session variables are set with the PHP global variable: $_SESSION
 PHP $_SESSION is an associative array that contains all session variables.
 session_start(); // for creating session
 $_SESSION["user"] = “abc"; //setting of session values
 echo $_SESSION["user"]; //get or printing session values
session_destroy() in php
 PHP session_destroy() function is used to destroy all session
variables completely.
//Example c.php
<?php
session_start();
session_destroy();
?>
Example for Session
// Abc.php
<?php
session_start(); //creation of session
?>
<html>
<body>
<?php
$_SESSION["user"] = “pavan"; //setting session
value
echo "Session information are set successfully.<br/>
";
?>
<a href=“cs.php">Visit next page</a>
</body>
</html>
// cs.php
<?php
session_start();
?>
<html>
<body>
<?php
echo "User is: ".$_SESSION["user"];
?>
</body>
</html>
Cookies in php
 PHP cookie is a small piece of information which is stored at client browser.
 It is used to recognize the user.
 Cookie is created at server side and saved to client browser.
 Each time when client sends request to the server, cookie is embedded with
request.
 In that way cookie can be received at the server side.
Example for cookies
setcookie() function
 setcookie() function is used to set cookie with HTTP response.
 Once cookie is set, you can access it by $_COOKIE superglobal
variable.
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
Example For COOKIES
<?php
setcookie("user", “aaa");
?>
<html>
<body>
<?php
if(!isset($_COOKIE["user"])) {
echo "Sorry, cookie is not found!";
} else {
echo "<br/>Cookie Value: " . $_COOKIE["user"];
}
?>
</body>
Difference b/w session and cookies

More Related Content

PPT
Html Ppt
PPT
Css lecture notes
PPT
Web Development using HTML & CSS
PPTX
Lab #2: Introduction to Javascript
PPTX
Php cookies
PPTX
html-table
PPTX
HTML Introduction, HTML History, HTML Uses, HTML benifits
PPTX
Javascript event handler
Html Ppt
Css lecture notes
Web Development using HTML & CSS
Lab #2: Introduction to Javascript
Php cookies
html-table
HTML Introduction, HTML History, HTML Uses, HTML benifits
Javascript event handler

What's hot (20)

PPTX
Introduction to php
PPTX
Event handling
PPTX
HTML (Web) basics for a beginner
PPSX
Introduction to Html5
PPTX
World Wide Web (WWW)
PPT
Introduction to html
PPTX
Php sessions
PPTX
Cascading Style Sheet (CSS)
PPTX
World wide web An Introduction
PPTX
(Fast) Introduction to HTML & CSS
PPTX
Html ppt
PPTX
Web Page Designing Using HTML
PPT
Hyperlinks in HTML
PPTX
Introduction to Html
PPTX
Java script cookies
PPT
Lecture 1 intro to web designing
PPTX
Internet Cookies
PPTX
Learn html Basics
PPT
Introduction to CSS
PPT
Introduction to Cascading Style Sheets (CSS)
Introduction to php
Event handling
HTML (Web) basics for a beginner
Introduction to Html5
World Wide Web (WWW)
Introduction to html
Php sessions
Cascading Style Sheet (CSS)
World wide web An Introduction
(Fast) Introduction to HTML & CSS
Html ppt
Web Page Designing Using HTML
Hyperlinks in HTML
Introduction to Html
Java script cookies
Lecture 1 intro to web designing
Internet Cookies
Learn html Basics
Introduction to CSS
Introduction to Cascading Style Sheets (CSS)
Ad

Similar to Sessions and cookies in php (20)

PPTX
PHP SESSIONS & COOKIE.pptx
PPT
Php - Getting good with session
PPT
PPT
Lecture8 php page control by okello erick
PPT
17 sessions
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
session.ppt
PPSX
Php session
PPT
season management in php (WT)
PPT
Php with my sql
PPTX
Php session 3 Important topics
PDF
4.4 PHP Session
PPT
Introducation to php for beginners
PPTX
PHP Cookies and Sessions
PPTX
18.register login
PDF
PHP-Cookies-Sessions.pdf
PPT
Manish
PHP SESSIONS & COOKIE.pptx
Php - Getting good with session
Lecture8 php page control by okello erick
17 sessions
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
session.ppt
Php session
season management in php (WT)
Php with my sql
Php session 3 Important topics
4.4 PHP Session
Introducation to php for beginners
PHP Cookies and Sessions
18.register login
PHP-Cookies-Sessions.pdf
Manish
Ad

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
01-Introduction-to-Information-Management.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PDF
Classroom Observation Tools for Teachers
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Pre independence Education in Inndia.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Computing-Curriculum for Schools in Ghana
PDF
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPH.pptx obstetrics and gynecology in nursing
102 student loan defaulters named and shamed – Is someone you know on the list?
01-Introduction-to-Information-Management.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Classroom Observation Tools for Teachers
Anesthesia in Laparoscopic Surgery in India
Pre independence Education in Inndia.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Supply Chain Operations Speaking Notes -ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Computing-Curriculum for Schools in Ghana
Microbial disease of the cardiovascular and lymphatic systems

Sessions and cookies in php

  • 2. Sessions in php  Session variables used to storing user information to be used across multiple pages  e.g. username, password, ..etc  By default, session variables last until the user closes the browser.
  • 4. Php session_start() function  A session is started with the session_start() function  Session variables are set with the PHP global variable: $_SESSION  PHP $_SESSION is an associative array that contains all session variables.  session_start(); // for creating session  $_SESSION["user"] = “abc"; //setting of session values  echo $_SESSION["user"]; //get or printing session values
  • 5. session_destroy() in php  PHP session_destroy() function is used to destroy all session variables completely. //Example c.php <?php session_start(); session_destroy(); ?>
  • 6. Example for Session // Abc.php <?php session_start(); //creation of session ?> <html> <body> <?php $_SESSION["user"] = “pavan"; //setting session value echo "Session information are set successfully.<br/> "; ?> <a href=“cs.php">Visit next page</a> </body> </html> // cs.php <?php session_start(); ?> <html> <body> <?php echo "User is: ".$_SESSION["user"]; ?> </body> </html>
  • 7. Cookies in php  PHP cookie is a small piece of information which is stored at client browser.  It is used to recognize the user.  Cookie is created at server side and saved to client browser.  Each time when client sends request to the server, cookie is embedded with request.  In that way cookie can be received at the server side.
  • 9. setcookie() function  setcookie() function is used to set cookie with HTTP response.  Once cookie is set, you can access it by $_COOKIE superglobal variable. <?php $cookie_name = "user"; $cookie_value = "John Doe"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day ?>
  • 10. Example For COOKIES <?php setcookie("user", “aaa"); ?> <html> <body> <?php if(!isset($_COOKIE["user"])) { echo "Sorry, cookie is not found!"; } else { echo "<br/>Cookie Value: " . $_COOKIE["user"]; } ?> </body>
  • 11. Difference b/w session and cookies