SlideShare a Scribd company logo
COOKIE AND SESSION IN PHP
By: Aashish Ghale
aashish.ghale@gmail.com
COOKIES
Like variable
 Stores single piece of data
under a unique name
 Browser dependent




stores cookies on user‟s browser

Moves page to page
 Avoids the problem to log in every time
 Always stored as text

COOKIES WITH PHP


Set cookie
setcookie() stores data in cookies
 Syntax: setcookie(“cookie name”,”value”);
 Accepts an optional third argument






E.g: setcookie(„username‟, „aacssh‟, time()+(60 * 60));




Sets the expiration date of the cookie
Sets expiration date 1 hour (60sec * 60min) from the current
time

Retrieve cookie


using super global using $_COOKIE

 E.g:

$_COOKIE[„cookie name‟];
(CONT…)
 Delete

cookie

Set

expiration date to time in the past
Syntax:
 setcookie(„username‟,‟aacssh‟, time()-3600);


Set time in the past
SESSION
Time spend by user at web application
 Stores small piece of data on server
 More secure and more reliable
 Automatically destroyed by shutting down the
browser
 Stored in session variable

SESSION WITH PHP


Starting session
 Started by function session_start()
Allow data to store in session variables
 Should be the first line of code




End session
 Close session with session_destroy()
(CONT…)


Session variables
 Set session variable with super global $_SESSION
 Once created , gets stored on the server
 E.g: $_SESSION[„variable‟] = value;
 Variable = name of session



Delete session variables manually
 Not

automatically deleted when session is destroyed
 Set $_SESSION superglobal to an empty array
 E.g: $_SESSION = array();
HOW PHP SESSION WORKS
USING BOTH SESSION AND COOKIE


Sessions have short life



Cookie can last forever




Long enough to outlast a session

If session uses cookie to remember session id


Cookie id is name after the session




i.e session_name()

To close session, cookie must also be deleted
SOMETHING MORE…..


Header function
Tells the server to send a new page
 Syntax: header(“Location: URL”);

CODE SNAPSHOTS :
1.

Combining session and cookie
1.

Creates session and cookie
CONTD…


session.php file


Separate file that is required in every php file.
CONTD


Logout.php


Destroies sessions and cookies
Lets make a simple login system

More Related Content

PPTX
PHP Cookies and Sessions
PPTX
Database Connectivity in PHP
PPT
Php with MYSQL Database
PPT
PPTX
PHP FUNCTIONS
PPTX
Php functions
PPT
PHP - Introduction to File Handling with PHP
PDF
Operators in PHP
PHP Cookies and Sessions
Database Connectivity in PHP
Php with MYSQL Database
PHP FUNCTIONS
Php functions
PHP - Introduction to File Handling with PHP
Operators in PHP

What's hot (20)

PPTX
Form using html and java script validation
PPT
PHP - Introduction to PHP AJAX
PDF
MySQL for beginners
PPTX
Ajax ppt - 32 slides
PPTX
Form Handling using PHP
PPT
PHP - Introduction to PHP Cookies and Sessions
PPTX
L21 io streams
PPT
Java Networking
PPT
PPTX
Date and time functions in mysql
PPT
MySQL Transactions
PPTX
C++ programming introduction
PDF
Python programming : List and tuples
PPT
Sql dml & tcl 2
PPTX
Php.ppt
PPTX
Loops PHP 04
PPT
Java Input Output and File Handling
PPT
14 file handling
 
PPTX
servlet in java
Form using html and java script validation
PHP - Introduction to PHP AJAX
MySQL for beginners
Ajax ppt - 32 slides
Form Handling using PHP
PHP - Introduction to PHP Cookies and Sessions
L21 io streams
Java Networking
Date and time functions in mysql
MySQL Transactions
C++ programming introduction
Python programming : List and tuples
Sql dml & tcl 2
Php.ppt
Loops PHP 04
Java Input Output and File Handling
14 file handling
 
servlet in java
Ad

Viewers also liked (10)

PPT
Css Ppt
PPT
cascading style sheet ppt
PPT
Introduction to Cascading Style Sheets (CSS)
PPT
CSS for Beginners
PPT
CSS Basics
PPT
How Cascading Style Sheets (CSS) Works
PPT
PPT
Introduction to CSS
PPTX
Cookies!
PDF
Html / CSS Presentation
Css Ppt
cascading style sheet ppt
Introduction to Cascading Style Sheets (CSS)
CSS for Beginners
CSS Basics
How Cascading Style Sheets (CSS) Works
Introduction to CSS
Cookies!
Html / CSS Presentation
Ad

Similar to Cookie and session (20)

PPSX
Sessions and cookies
PPTX
FP512 Cookies sessions
PDF
PHP-Cookies-Sessions.pdf
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPT
Lecture8 php page control by okello erick
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPTX
javaScriptCookies.pptx
PPT
PDF
4.4 PHP Session
PDF
Introduction to php web programming - sessions and cookies
PPTX
Cookies
PPT
Php ssession - cookies -introduction
PPT
Manish
PPTX
lecture 12.pptx
PDF
Cookies and sessions
PPTX
Cookies in php
PPTX
Sessions and cookies in php
Sessions and cookies
FP512 Cookies sessions
PHP-Cookies-Sessions.pdf
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Lecture8 php page control by okello erick
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
javaScriptCookies.pptx
4.4 PHP Session
Introduction to php web programming - sessions and cookies
Cookies
Php ssession - cookies -introduction
Manish
lecture 12.pptx
Cookies and sessions
Cookies in php
Sessions and cookies in php

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
cloud_computing_Infrastucture_as_cloud_p
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Approach and Philosophy of On baking technology
PDF
DP Operators-handbook-extract for the Mautical Institute
PPTX
A Presentation on Artificial Intelligence
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
MIND Revenue Release Quarter 2 2025 Press Release
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
OMC Textile Division Presentation 2021.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Encapsulation theory and applications.pdf
Tartificialntelligence_presentation.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Heart disease approach using modified random forest and particle swarm optimi...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Getting Started with Data Integration: FME Form 101
Hindi spoken digit analysis for native and non-native speakers
cloud_computing_Infrastucture_as_cloud_p
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
A novel scalable deep ensemble learning framework for big data classification...
Approach and Philosophy of On baking technology
DP Operators-handbook-extract for the Mautical Institute
A Presentation on Artificial Intelligence
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...

Cookie and session

  • 1. COOKIE AND SESSION IN PHP By: Aashish Ghale aashish.ghale@gmail.com
  • 2. COOKIES Like variable  Stores single piece of data under a unique name  Browser dependent   stores cookies on user‟s browser Moves page to page  Avoids the problem to log in every time  Always stored as text 
  • 3. COOKIES WITH PHP  Set cookie setcookie() stores data in cookies  Syntax: setcookie(“cookie name”,”value”);  Accepts an optional third argument    E.g: setcookie(„username‟, „aacssh‟, time()+(60 * 60));   Sets the expiration date of the cookie Sets expiration date 1 hour (60sec * 60min) from the current time Retrieve cookie  using super global using $_COOKIE  E.g: $_COOKIE[„cookie name‟];
  • 4. (CONT…)  Delete cookie Set expiration date to time in the past Syntax:  setcookie(„username‟,‟aacssh‟, time()-3600);  Set time in the past
  • 5. SESSION Time spend by user at web application  Stores small piece of data on server  More secure and more reliable  Automatically destroyed by shutting down the browser  Stored in session variable 
  • 6. SESSION WITH PHP  Starting session  Started by function session_start() Allow data to store in session variables  Should be the first line of code   End session  Close session with session_destroy()
  • 7. (CONT…)  Session variables  Set session variable with super global $_SESSION  Once created , gets stored on the server  E.g: $_SESSION[„variable‟] = value;  Variable = name of session  Delete session variables manually  Not automatically deleted when session is destroyed  Set $_SESSION superglobal to an empty array  E.g: $_SESSION = array();
  • 9. USING BOTH SESSION AND COOKIE  Sessions have short life  Cookie can last forever   Long enough to outlast a session If session uses cookie to remember session id  Cookie id is name after the session   i.e session_name() To close session, cookie must also be deleted
  • 10. SOMETHING MORE…..  Header function Tells the server to send a new page  Syntax: header(“Location: URL”); 
  • 11. CODE SNAPSHOTS : 1. Combining session and cookie 1. Creates session and cookie
  • 12. CONTD…  session.php file  Separate file that is required in every php file.
  • 14. Lets make a simple login system