Ch4(saving state with cookies and query strings)
-
Upload
chhom-karath -
Category
Technology
-
view
6 -
download
1
Transcript of Ch4(saving state with cookies and query strings)
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>\AppData\Roaming\Microsoft\Windows\Cookies\C:\Users\<username>\AppData\Roaming\Microsoft\Windows\Cookies\Low\
EX:<?phpsetcookie("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 connectionsetcookie( "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: <?phpsession_start();print "<p>Welcome, your session ID is ".session_id()."</p>\n\n";?>
– 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($_SESSION['products'], $data));
if(is_array($_SESSION['products'])){
foreach($_SESSION['products'] as $p){
print "$p </br>";}
}?>
Destroying Sessions and Unsetting Elements<?phpsession_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'] );