SlideShare a Scribd company logo
FP512 
WEB PROGRAMMING I 
COOKIES 
& 
SESSION 
Prepared by: PN. ZATI HANANI ZAINAL
UNDERSTAND COOKIES & SESSIONS 
Define Cookies 
Create Cookies 
Retrieve the value of a cookies 
Delete cookies 
Define sessions 
Create session 
Delete session 
Prepared by: PN. ZATI HANANI ZAINAL
COOKIES 
Prepared by: PN. ZATI HANANI ZAINAL
WHAT IS COOKIES? 
A cookie is often used to identify a user. 
Embed on user PC (client). 
To track certain user details like (No. Of Visits, names, 
last visit, etc). 
The client machine stores such information and sends 
it to the web server whenever there is a request. 
 Cookies data are sent along with the HTTP headers. 
Prepared by: PN. ZATI HANANI ZAINAL
COOKIES ARE POPULAR?? 
Most popular uses of cookies? 
To store username/password information so user 
doesn't have to log in every time ("remember me"). 
To simply remember the user's name. 
To keep track of a users progress during a specified 
process. 
To remember a users theme. 
Prepared by: PN. ZATI HANANI ZAINAL
HOW TO CREATE A COOKIE? 
The setcookie() function is used to set a cookie. 
SYNTAX 
setcookie(name, value, expire, path, domain); 
name: The name of your cookie. 
value: The value that is stored in your cookie. 
expiration: The date when the cookie will expire and be deleted. 
Note: 
The setcookie() function must appear BEFORE the <html> tag. 
Prepared by: PN. ZATI HANANI ZAINAL
HOW TO CREATE A COOKIE? 
Example 
setcookie(name, value, expire); 
setcookies(“user”,”fiza”,time()+300); 
If(isset(&_COOKIE[“user”])) 
echo “Hai” . &_COOKIE[“user”] . “<br>” 
else 
echo “Welcome guest!!<br>”; 
NOTE : If you do not set this expiration date, the 
cookie will removed when the browser is restarted. Prepared by: PN. ZATI HANANI ZAINAL
HOW TO CREATE A COOKIE? 
EXAMPLE 1 
We will create a cookie named "user" and assign 
the value "Alex Porter" to it. We also specify 
that the cookie should expire after one hour: 
<?php 
setcookie("user", "Alex Porter", time()+3600); 
?> 
<html> 
..... 
SYNTAX 
Note: The value of the cookie is automatically URLencoded when sending the cookie, 
Prepared by: PN. ZATI HANANI ZAINAL 
and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
HOW TO CREATE A COOKIE? 
EXAMPLE 2 
Set the expiration time of the cookie in another way. It 
may be easier than using seconds. 
<?php 
$expire=time()+60*60*24*30; 
setcookie("user", "Alex Porter", $expire); 
?> 
<html> 
..... 
SYNTAX 
Note: The expiration time is set to a month (Prepared by: PN. ZATI HANANI ZAINAL 60 sec * 60 min * 24 hours * 30 days).
HOW TO RETRIEVE A COOKIE VALUE? 
The PHP $_COOKIE variable is used to retrieve a 
cookie value. 
<?php 
// Print a cookie 
echo $_COOKIE["user"]; 
// A way to view all cookies 
print_r($_COOKIE); 
?> 
SYNTAX 
Prepared by: PN. ZATI HANANI ZAINAL
HOW TO RETRIEVE A COOKIE VALUE? 
EXAMPLE 2 
Use the isset() function to find out if a cookie has 
been set 
<html> 
<body> 
<?php 
if (isset($_COOKIE["user"])) 
echo "Welcome " . $_COOKIE["user"] . "!<br />"; 
else 
echo "Welcome guest!<br />"; 
?> 
</body> 
</html> 
SYNTAX 
Prepared by: PN. ZATI HANANI ZAINAL
HOW TO DELETE COOKIES 
When deleting a cookie you should assure 
that the expiration date is in the past. 
<?php 
// set the expiration date to one hour ago 
setcookie("user", "", time()-3600); 
?> 
SYNTAX 
Prepared by: PN. ZATI HANANI ZAINAL
SESSIONS 
Prepared by: PN. ZATI HANANI ZAINAL
WHAT IS SESSIONS? 
 A PHP session variable is used to store information about, or 
change settings for a user session. 
 Session variables hold information about one single user, and 
are available to all pages in one application. 
 PHP session solves this problem by allowing you to store 
user information on the server for later use. 
 Use of session: 
1) Login function 
2) Shopping cart 
Prepared by: PN. ZATI HANANI ZAINAL
STARTING A PHP SESSION 
Before you can store user information in your PHP session, you must first 
start up the session. 
The code below will register the user's session with the server, allow you 
to start saving user information, and assign a UID for that user's session. 
SYNTAX 
<?php 
session_start(); 
?> 
<html> 
<body></body> 
</html> 
Prepared by: PN. ZATI HANANI ZAINAL 
Note: The session_start() function must appear BEFORE the <html> tag
STORING SESSION VARIABLE 
<?php 
EXAMPLE 
session_start(); 
$_SESSION['views']=1; // store session data 
?> 
<html> 
<body> 
<?php 
//retrieve session data 
echo "Pageviews=". $_SESSION['views']; 
?> 
</body> 
</html> 
Prepared by: PN. ZATI HANANI ZAINAL
DESTROYING A SESSIONS 
To delete some session data, you can use the unset() or the 
session_destroy() function. 
The unset() function is used to free the specified session variable 
<?php 
unset($_SESSION['views']); 
?> 
SYNTAX 
Prepared by: PN. ZATI HANANI ZAINAL
DESTROYING A SESSIONS 
Completely destroy the session by calling the session_destroy() 
function 
<?php 
session_destroy(); 
?> 
SYNTAX 
Prepared by: PN. ZATI HANANI ZAINAL 
Note: session_destroy() will reset your session and you will lose all your stored session data.
THE END 
Prepared by: PN. ZATI HANANI ZAINAL

More Related Content

PPT
Php ssession - cookies -introduction
Programmer Blog
 
PPTX
Cookie and session
Aashish Ghale
 
PPTX
PHP Cookies and Sessions
Nisa Soomro
 
PPT
Cookies and sessions
UdaAs PaNchi
 
PPT
PHP - Introduction to PHP Cookies and Sessions
Vibrant Technologies & Computers
 
PPT
Lecture8 php page control by okello erick
okelloerick
 
ODP
Session Management & Cookies In Php
Harit Kothari
 
Php ssession - cookies -introduction
Programmer Blog
 
Cookie and session
Aashish Ghale
 
PHP Cookies and Sessions
Nisa Soomro
 
Cookies and sessions
UdaAs PaNchi
 
PHP - Introduction to PHP Cookies and Sessions
Vibrant Technologies & Computers
 
Lecture8 php page control by okello erick
okelloerick
 
Session Management & Cookies In Php
Harit Kothari
 

What's hot (20)

PPSX
Php session
argusacademy
 
PPTX
Sessions in php
Mudasir Syed
 
PDF
Introduction to php web programming - sessions and cookies
baabtra.com - No. 1 supplier of quality freshers
 
PPT
PHP Cookies, Sessions and Authentication
Gerard Sychay
 
PPT
PHP - Getting good with cookies
Firdaus Adib
 
PPTX
Session php
200Hussain
 
PDF
Magento2&amp;java script (2)
EvgeniyKapelko1
 
PPTX
Php cookies
JIGAR MAKHIJA
 
PDF
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe
 
PPT
Web Cookies
apwebco
 
PPTX
Cookies
amuthadeepa
 
PPTX
Php sessions
JIGAR MAKHIJA
 
KEY
Authentication
soon
 
PDF
The Dean wants to Make this WordPress Site Responsive
Joe Casabona
 
PPT
Cookies and sessions
Lena Petsenchuk
 
PDF
WCCHS: Responsive Design with WordPress
Joe Casabona
 
PPT
Responsive Design with WordPress (WCPHX)
Joe Casabona
 
PDF
Odoo Experience 2018 - From a Web Controller to a Full CMS
ElínAnna Jónasdóttir
 
PDF
Odoo Experience 2018 - Develop an App with the Odoo Framework
ElínAnna Jónasdóttir
 
Php session
argusacademy
 
Sessions in php
Mudasir Syed
 
Introduction to php web programming - sessions and cookies
baabtra.com - No. 1 supplier of quality freshers
 
PHP Cookies, Sessions and Authentication
Gerard Sychay
 
PHP - Getting good with cookies
Firdaus Adib
 
Session php
200Hussain
 
Magento2&amp;java script (2)
EvgeniyKapelko1
 
Php cookies
JIGAR MAKHIJA
 
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe
 
Web Cookies
apwebco
 
Cookies
amuthadeepa
 
Php sessions
JIGAR MAKHIJA
 
Authentication
soon
 
The Dean wants to Make this WordPress Site Responsive
Joe Casabona
 
Cookies and sessions
Lena Petsenchuk
 
WCCHS: Responsive Design with WordPress
Joe Casabona
 
Responsive Design with WordPress (WCPHX)
Joe Casabona
 
Odoo Experience 2018 - From a Web Controller to a Full CMS
ElínAnna Jónasdóttir
 
Odoo Experience 2018 - Develop an App with the Odoo Framework
ElínAnna Jónasdóttir
 
Ad

Similar to FP512 Cookies sessions (20)

PPTX
lecture 12.pptx
ITNet
 
PPT
Manish
Manish Jain
 
PPT
cookies.ppt
SchoolEducationDepar
 
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
SreejithVP7
 
PPTX
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
pondypaiyan
 
PDF
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
PPTX
PHP COOKIES AND SESSIONS
Degu8
 
PPSX
Sessions and cookies
www.netgains.org
 
PPTX
4 php-advanced
Achchuthan Yogarajah
 
PPT
Session,cookies
rkmourya511
 
PDF
4.4 PHP Session
Jalpesh Vasa
 
PPT
Cookies & Session
university of education,Lahore
 
PPTX
murach12.pptx
xiso
 
PPTX
javaScriptCookies.pptx
MattMarino13
 
PPTX
Cookie & Session In ASP.NET
ShingalaKrupa
 
PDF
Cookies and sessions
salissal
 
PPT
season management in php (WT)
kunjan shah
 
PPTX
lecture 13.pptx
ITNet
 
PPTX
Session tracking in servlets
vishal choudhary
 
lecture 12.pptx
ITNet
 
Manish
Manish Jain
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
SreejithVP7
 
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
pondypaiyan
 
PHP-Cookies-Sessions.pdf
HumphreyOwuor1
 
PHP COOKIES AND SESSIONS
Degu8
 
Sessions and cookies
www.netgains.org
 
4 php-advanced
Achchuthan Yogarajah
 
Session,cookies
rkmourya511
 
4.4 PHP Session
Jalpesh Vasa
 
murach12.pptx
xiso
 
javaScriptCookies.pptx
MattMarino13
 
Cookie & Session In ASP.NET
ShingalaKrupa
 
Cookies and sessions
salissal
 
season management in php (WT)
kunjan shah
 
lecture 13.pptx
ITNet
 
Session tracking in servlets
vishal choudhary
 
Ad

Recently uploaded (20)

PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 

FP512 Cookies sessions

  • 1. FP512 WEB PROGRAMMING I COOKIES & SESSION Prepared by: PN. ZATI HANANI ZAINAL
  • 2. UNDERSTAND COOKIES & SESSIONS Define Cookies Create Cookies Retrieve the value of a cookies Delete cookies Define sessions Create session Delete session Prepared by: PN. ZATI HANANI ZAINAL
  • 3. COOKIES Prepared by: PN. ZATI HANANI ZAINAL
  • 4. WHAT IS COOKIES? A cookie is often used to identify a user. Embed on user PC (client). To track certain user details like (No. Of Visits, names, last visit, etc). The client machine stores such information and sends it to the web server whenever there is a request.  Cookies data are sent along with the HTTP headers. Prepared by: PN. ZATI HANANI ZAINAL
  • 5. COOKIES ARE POPULAR?? Most popular uses of cookies? To store username/password information so user doesn't have to log in every time ("remember me"). To simply remember the user's name. To keep track of a users progress during a specified process. To remember a users theme. Prepared by: PN. ZATI HANANI ZAINAL
  • 6. HOW TO CREATE A COOKIE? The setcookie() function is used to set a cookie. SYNTAX setcookie(name, value, expire, path, domain); name: The name of your cookie. value: The value that is stored in your cookie. expiration: The date when the cookie will expire and be deleted. Note: The setcookie() function must appear BEFORE the <html> tag. Prepared by: PN. ZATI HANANI ZAINAL
  • 7. HOW TO CREATE A COOKIE? Example setcookie(name, value, expire); setcookies(“user”,”fiza”,time()+300); If(isset(&_COOKIE[“user”])) echo “Hai” . &_COOKIE[“user”] . “<br>” else echo “Welcome guest!!<br>”; NOTE : If you do not set this expiration date, the cookie will removed when the browser is restarted. Prepared by: PN. ZATI HANANI ZAINAL
  • 8. HOW TO CREATE A COOKIE? EXAMPLE 1 We will create a cookie named "user" and assign the value "Alex Porter" to it. We also specify that the cookie should expire after one hour: <?php setcookie("user", "Alex Porter", time()+3600); ?> <html> ..... SYNTAX Note: The value of the cookie is automatically URLencoded when sending the cookie, Prepared by: PN. ZATI HANANI ZAINAL and automatically decoded when received (to prevent URLencoding, use setrawcookie() instead).
  • 9. HOW TO CREATE A COOKIE? EXAMPLE 2 Set the expiration time of the cookie in another way. It may be easier than using seconds. <?php $expire=time()+60*60*24*30; setcookie("user", "Alex Porter", $expire); ?> <html> ..... SYNTAX Note: The expiration time is set to a month (Prepared by: PN. ZATI HANANI ZAINAL 60 sec * 60 min * 24 hours * 30 days).
  • 10. HOW TO RETRIEVE A COOKIE VALUE? The PHP $_COOKIE variable is used to retrieve a cookie value. <?php // Print a cookie echo $_COOKIE["user"]; // A way to view all cookies print_r($_COOKIE); ?> SYNTAX Prepared by: PN. ZATI HANANI ZAINAL
  • 11. HOW TO RETRIEVE A COOKIE VALUE? EXAMPLE 2 Use the isset() function to find out if a cookie has been set <html> <body> <?php if (isset($_COOKIE["user"])) echo "Welcome " . $_COOKIE["user"] . "!<br />"; else echo "Welcome guest!<br />"; ?> </body> </html> SYNTAX Prepared by: PN. ZATI HANANI ZAINAL
  • 12. HOW TO DELETE COOKIES When deleting a cookie you should assure that the expiration date is in the past. <?php // set the expiration date to one hour ago setcookie("user", "", time()-3600); ?> SYNTAX Prepared by: PN. ZATI HANANI ZAINAL
  • 13. SESSIONS Prepared by: PN. ZATI HANANI ZAINAL
  • 14. WHAT IS SESSIONS?  A PHP session variable is used to store information about, or change settings for a user session.  Session variables hold information about one single user, and are available to all pages in one application.  PHP session solves this problem by allowing you to store user information on the server for later use.  Use of session: 1) Login function 2) Shopping cart Prepared by: PN. ZATI HANANI ZAINAL
  • 15. STARTING A PHP SESSION Before you can store user information in your PHP session, you must first start up the session. The code below will register the user's session with the server, allow you to start saving user information, and assign a UID for that user's session. SYNTAX <?php session_start(); ?> <html> <body></body> </html> Prepared by: PN. ZATI HANANI ZAINAL Note: The session_start() function must appear BEFORE the <html> tag
  • 16. STORING SESSION VARIABLE <?php EXAMPLE session_start(); $_SESSION['views']=1; // store session data ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html> Prepared by: PN. ZATI HANANI ZAINAL
  • 17. DESTROYING A SESSIONS To delete some session data, you can use the unset() or the session_destroy() function. The unset() function is used to free the specified session variable <?php unset($_SESSION['views']); ?> SYNTAX Prepared by: PN. ZATI HANANI ZAINAL
  • 18. DESTROYING A SESSIONS Completely destroy the session by calling the session_destroy() function <?php session_destroy(); ?> SYNTAX Prepared by: PN. ZATI HANANI ZAINAL Note: session_destroy() will reset your session and you will lose all your stored session data.
  • 19. THE END Prepared by: PN. ZATI HANANI ZAINAL