SlideShare a Scribd company logo
Saving State with Cookies and
Saving State with Session
Functions
• HTTP is a stateless protocol. Therefore, every page a user downloads
from your server represents a separate connection.
• On the other hand, Web sites are perceived by users and publishers
alike as environments, as spaces within which a single page is part of
a wider whole.
• It's not surprising, therefore, that strategies to pass information from
page to page are as old as the Web itself.
• two methods of storing information on one page that can then be
accessed on subsequent pages.
Cookies
• Cookies are simply a way for a server to store information on the user’s computer.
By doing so, the server can remember the user over the course of a visit or through
several Visits.
• A cookie is a small amount of data stored by the user's browser in compliance with
a request from a server or script. A host can request that up to 20 cookies be stored
by a user's browser. Each cookie consists of a name, a value, and an expiry date, as
well as host and path information. An individual cookie is limited to 4KB.
• After a cookie is set, only the originating host can read the data, ensuring that the
user's privacy is respected. cookies can be an excellent way of saving small
amounts of information about a user from page to page or even from visit to visit.
C:Users<username>AppDataRoamingMicrosoftWindowsCookies
C:Users<username>AppDataRoamingMicrosoftWindowsCookiesLow
EX:
<?php
setcookie("cookie[three]","cookiethree");
setcookie("cookie[two]","cookietwo");
setcookie("cookie[one]","cookieone");
// print cookies (after reloading page)
if (isset($_COOKIE["cookie"]))
{
foreach ($_COOKIE["cookie"] as $name => $value)
{
echo "$name : $value <br />";
}
}
?>
<html>
<body>
The Anatomy of a Cookie
Setting a Cookie with PHP(setcookie(name[,value,Expiredate, path,domain[))
setcookie( "vegetable", "artichoke", time()+3600, "/", "example.com", 0 );
//1 if the cookie is to be sent only over a secure connection
setcookie( "vegetable", "artichoke", time()+3600 ); //Current domain will be used implicitly:
EX:
<?php
setcookie( "vegetable", "artichoke", time()+3600, "/" );
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Listing 19.1 Setting and Printing a Cookie Value</title>
</head>
<body>
<?php
if ( isset( $_COOKIE['vegetable'] ) ) {
print "<p>Hello again, your chosen vegetable is ";
print "{$_COOKIE['vegetable']}</p>";
} else {
print "<p>Hello you. This may be your first visit</p>";
}
?>
</body>
</html>
EX:
<?php
date_default_timezone_set("asia/phnom_penh");
setcookie("lastVisit",Date("G:i a - F/d/Y"), time()+3600);
if(isset($_COOKIE['lastVisit']))
{
echo "Last visit is " . $_COOKIE['lastVisit'];
}
?>
<html><head><title></title></head><body>
<h1>My site</h1>
</body>
</html>
Saving State with Session Functions
• session data becomes available in the global namespace.
• you can access them through the superglobal $_SESSION associative array.
• Session state is usually stored in a temporary file.//session.save_path = "/tmp“
//print session_save_path();
• By default, sessions do not start automatically.//session.auto_start = 0
– EX:
<?php
session_start();
print "<p>Welcome, your session ID is ".session_id()."</p>nn";?>
– EX:
EX:
<?php
session_start();
?>
<?php
$_SESSION['product1'] = "Sonic Screwdriver";
$_SESSION['product2'] = "HAL 2000";
?>
=>
<?php
session_start();
?>
<?php
print $_SESSION['product1'];
print $_SESSION['product2'];
?>
EX:
<?php
session_start();
?>
<?php
$data=array(1,2,3,4,5,6,7);
$_SESSION['products']=array();
//$_SESSION['products']=$data;
$_SESSION['products']=array_unique(array_merge($_SESSI
ON['products'], $data));
if(is_array($_SESSION['products']))
{
foreach($_SESSION['products'] as $p)
{
print "$p </br>";
}
}
?>
Destroying Sessions and Unsetting Elements
<?php
session_start();
$_SESSION['test'] = 5;
session_destroy();
$_SESSION=array();
print $_SESSION['test']; // prints nothing. The test element is no more
?>
Note: You can remove individual elements by calling unset() on them, like so:
unset( $_SESSION['test'] );

More Related Content

PDF
Introduction to php web programming - sessions and cookies
PPTX
Cookies and sessions
PPT
PPTX
Session and Cookies
PPSX
Sessions and cookies
PPT
PHP Cookies, Sessions and Authentication
PPTX
Cookie and session
PPTX
PHP Cookies and Sessions
Introduction to php web programming - sessions and cookies
Cookies and sessions
Session and Cookies
Sessions and cookies
PHP Cookies, Sessions and Authentication
Cookie and session
PHP Cookies and Sessions

What's hot (20)

PPT
Php ssession - cookies -introduction
PPTX
java Cookies
PPT
PHP - Getting good with cookies
PPT
PHP - Introduction to PHP Cookies and Sessions
PPT
Web Cookies
PPTX
PPT
Cookies and sessions
PPTX
Javascript ch8
PPTX
Cookie & Session In ASP.NET
PPTX
Sessions in php
PPTX
Cookies and Session
PPT
Cookies and sessions
PPT
16 cookies
PPTX
Cookies-PHP
PPTX
Php Basics part 1
PPTX
Using cookies and sessions
PDF
Prerendering Chapter 0
PPT
Php Sessoins N Cookies
ODP
ClusterDesktop how-to autostart on Mac
Php ssession - cookies -introduction
java Cookies
PHP - Getting good with cookies
PHP - Introduction to PHP Cookies and Sessions
Web Cookies
Cookies and sessions
Javascript ch8
Cookie & Session In ASP.NET
Sessions in php
Cookies and Session
Cookies and sessions
16 cookies
Cookies-PHP
Php Basics part 1
Using cookies and sessions
Prerendering Chapter 0
Php Sessoins N Cookies
ClusterDesktop how-to autostart on Mac
Ad

Viewers also liked (20)

PPT
Chapter 10 Synchronous Communication
PPTX
Voice
PDF
Groundwater Research and Technology, Stefan Schuster
PPT
Php i-slides
PPT
Chapter 1Into the Internet
PDF
Appendex d
PDF
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
PPTX
Ch3(working with file)
PPT
PHP 5.3 Part 1 - Introduction to PHP 5.3
PDF
Appendex f
PPTX
CIS 110 Chapter 1 Intro to Computers
PDF
Drought: Looking Back and Planning Ahead, Todd Votteler
PPT
PHP - Introduction to PHP - Mazenet Solution
PDF
Appendex g
PPT
Chapter 9 Asynchronous Communication
PPT
PHP - Introduction to PHP Date and Time Functions
PPT
Chapter 4 Form Factors Power Supplies
PPT
Unix Master
PPTX
Setting up a gmail account
PDF
Ch07
Chapter 10 Synchronous Communication
Voice
Groundwater Research and Technology, Stefan Schuster
Php i-slides
Chapter 1Into the Internet
Appendex d
WE1.L10 - GRACE Applications to Regional Hydrology and Water Resources
Ch3(working with file)
PHP 5.3 Part 1 - Introduction to PHP 5.3
Appendex f
CIS 110 Chapter 1 Intro to Computers
Drought: Looking Back and Planning Ahead, Todd Votteler
PHP - Introduction to PHP - Mazenet Solution
Appendex g
Chapter 9 Asynchronous Communication
PHP - Introduction to PHP Date and Time Functions
Chapter 4 Form Factors Power Supplies
Unix Master
Setting up a gmail account
Ch07
Ad

Similar to Ch4(saving state with cookies and query strings) (20)

PPTX
19_JavaScript - Storage_Cookies-tutorial .pptx
PPTX
PHP COOKIES AND SESSIONS
PPT
Session,cookies
PPTX
19_JavaScript - Storage_Cookies_students.pptx
PPTX
Overview of Cookies in HTTP - Miran al Mehrab
PDF
Web app development_cookies_sessions_14
PPTX
javaScriptCookies.pptx
PPTX
APEX & Cookie Monster
PDF
PHP-Cookies-Sessions.pdf
PPTX
lecture 12.pptx
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PDF
ASP.NET-Web Programming - Sessions and Cookies
PPTX
Php cookies
PDF
Lt local storage
PPTX
Cookies
PPT
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
PPTX
Cookies
PPT
Lecture8 php page control by okello erick
PPTX
Internet Cookies
19_JavaScript - Storage_Cookies-tutorial .pptx
PHP COOKIES AND SESSIONS
Session,cookies
19_JavaScript - Storage_Cookies_students.pptx
Overview of Cookies in HTTP - Miran al Mehrab
Web app development_cookies_sessions_14
javaScriptCookies.pptx
APEX & Cookie Monster
PHP-Cookies-Sessions.pdf
lecture 12.pptx
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
ASP.NET-Web Programming - Sessions and Cookies
Php cookies
Lt local storage
Cookies
Lecture 11 - PHP - Part 5 - CookiesSessions.ppt
Cookies
Lecture8 php page control by okello erick
Internet Cookies

More from Chhom Karath (20)

PDF
set1.pdf
PPTX
Set1.pptx
PDF
orthodontic patient education.pdf
PDF
New ton 3.pdf
PPTX
ច្បាប់ញូតុនទី៣.pptx
PPTX
Control tipping.pptx
PPTX
Bulbous loop.pptx
PPTX
brush teeth.pptx
PPTX
bracket size.pptx
PPTX
arch form KORI copy.pptx
PPTX
Bracket size
PPTX
Couple
PPTX
ច្បាប់ញូតុនទី៣
PPTX
PPTX
Shoe horn loop
PPTX
Opus loop
PPTX
V bend
PPTX
Closing loop
PPTX
Maxillary arch form
PPTX
Front face analysis
set1.pdf
Set1.pptx
orthodontic patient education.pdf
New ton 3.pdf
ច្បាប់ញូតុនទី៣.pptx
Control tipping.pptx
Bulbous loop.pptx
brush teeth.pptx
bracket size.pptx
arch form KORI copy.pptx
Bracket size
Couple
ច្បាប់ញូតុនទី៣
Shoe horn loop
Opus loop
V bend
Closing loop
Maxillary arch form
Front face analysis

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Getting Started with Data Integration: FME Form 101
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
1. Introduction to Computer Programming.pptx
PPTX
Big Data Technologies - Introduction.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Approach and Philosophy of On baking technology
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
cuic standard and advanced reporting.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Encapsulation_ Review paper, used for researhc scholars
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
NewMind AI Weekly Chronicles - August'25-Week II
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Getting Started with Data Integration: FME Form 101
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Spectroscopy.pptx food analysis technology
1. Introduction to Computer Programming.pptx
Big Data Technologies - Introduction.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
A comparative analysis of optical character recognition models for extracting...
Approach and Philosophy of On baking technology
“AI and Expert System Decision Support & Business Intelligence Systems”
Network Security Unit 5.pdf for BCA BBA.
cuic standard and advanced reporting.pdf
Programs and apps: productivity, graphics, security and other tools
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...

Ch4(saving state with cookies and query strings)

  • 1. Saving State with Cookies and Saving State with Session Functions
  • 2. • HTTP is a stateless protocol. Therefore, every page a user downloads from your server represents a separate connection. • On the other hand, Web sites are perceived by users and publishers alike as environments, as spaces within which a single page is part of a wider whole. • It's not surprising, therefore, that strategies to pass information from page to page are as old as the Web itself. • two methods of storing information on one page that can then be accessed on subsequent pages.
  • 3. Cookies • Cookies are simply a way for a server to store information on the user’s computer. By doing so, the server can remember the user over the course of a visit or through several Visits. • A cookie is a small amount of data stored by the user's browser in compliance with a request from a server or script. A host can request that up to 20 cookies be stored by a user's browser. Each cookie consists of a name, a value, and an expiry date, as well as host and path information. An individual cookie is limited to 4KB. • After a cookie is set, only the originating host can read the data, ensuring that the user's privacy is respected. cookies can be an excellent way of saving small amounts of information about a user from page to page or even from visit to visit.
  • 5. The Anatomy of a Cookie
  • 6. Setting a Cookie with PHP(setcookie(name[,value,Expiredate, path,domain[)) setcookie( "vegetable", "artichoke", time()+3600, "/", "example.com", 0 ); //1 if the cookie is to be sent only over a secure connection setcookie( "vegetable", "artichoke", time()+3600 ); //Current domain will be used implicitly: EX: <?php setcookie( "vegetable", "artichoke", time()+3600, "/" ); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Listing 19.1 Setting and Printing a Cookie Value</title> </head> <body> <?php if ( isset( $_COOKIE['vegetable'] ) ) { print "<p>Hello again, your chosen vegetable is "; print "{$_COOKIE['vegetable']}</p>"; } else { print "<p>Hello you. This may be your first visit</p>"; } ?> </body> </html>
  • 7. EX: <?php date_default_timezone_set("asia/phnom_penh"); setcookie("lastVisit",Date("G:i a - F/d/Y"), time()+3600); if(isset($_COOKIE['lastVisit'])) { echo "Last visit is " . $_COOKIE['lastVisit']; } ?> <html><head><title></title></head><body> <h1>My site</h1> </body> </html>
  • 8. Saving State with Session Functions • session data becomes available in the global namespace. • you can access them through the superglobal $_SESSION associative array. • Session state is usually stored in a temporary file.//session.save_path = "/tmp“ //print session_save_path(); • By default, sessions do not start automatically.//session.auto_start = 0 – EX: <?php session_start(); print "<p>Welcome, your session ID is ".session_id()."</p>nn";?> – EX:
  • 9. EX: <?php session_start(); ?> <?php $_SESSION['product1'] = "Sonic Screwdriver"; $_SESSION['product2'] = "HAL 2000"; ?> => <?php session_start(); ?> <?php print $_SESSION['product1']; print $_SESSION['product2']; ?> EX: <?php session_start(); ?> <?php $data=array(1,2,3,4,5,6,7); $_SESSION['products']=array(); //$_SESSION['products']=$data; $_SESSION['products']=array_unique(array_merge($_SESSI ON['products'], $data)); if(is_array($_SESSION['products'])) { foreach($_SESSION['products'] as $p) { print "$p </br>"; } } ?>
  • 10. Destroying Sessions and Unsetting Elements <?php session_start(); $_SESSION['test'] = 5; session_destroy(); $_SESSION=array(); print $_SESSION['test']; // prints nothing. The test element is no more ?> Note: You can remove individual elements by calling unset() on them, like so: unset( $_SESSION['test'] );

Editor's Notes

  • #10: <?php session_start(); if ( empty( $_SESSION['products'] ) ) { $_SESSION['products']=array(); } if ( is_array( $_REQUEST['form_products'] ) ) { $_SESSION['products'] = array_unique( array_merge( $_SESSION['products'], $_REQUEST['form_products'] ) ); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Listing 20.4 Registering an Array Element with a Session</title> </head> <body> <div> <form action="<?php print $_SERVER['PHP_SELF']?>" method="post"> <p> <select name="form_subject[]" multiple="multiple" size="5"> <option>Sonic Screwdriver</option> <option>Hal 2000</option> <option>Tardis</option> <option>ORAC</option> <option>Transporter bracelet</option> </select> </p> <p> <input type="submit" value="choose" /> </p> </form> <a href="2.php">A content page</a> </div> </body> </html> //-------------------------------------------- <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <title>Listing 20.5 Accessing Session Elements</title> </head> <body> <div> <h1>A Content Page</h1> <?php if ( is_array( $_SESSION['products'] ) ) { print "<b>Your cart:</b><ol>\n"; foreach ( $_SESSION['products'] as $p ) { print "<li>$p</li>"; } print "</ol>"; } ?> <a href="1.php">Back to product choice page</a> </div> </body> </html>