Bài giảng Lập trình Web - Chapter 7: Cookies and Sessions - Nguyễn Hữu Hiếu

Chapter 7  
Cookies and Sessions  
Lectured by:  
Nguyễn Hữu Hiếu  
The need for persistence  
l
Consider these examples  
− Counting the number of “hits” on a website  
− i.e. how many times does a client load your web page  
source  
− The questionnaire on computing experience  
l
Somehow your .php needs to remember previous  
instances of it being requested by a client  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
2
Persistence  
l
Persistence is the ability of data to outlive the execution  
of the program that created them.  
l
An obvious way of achieving persistence is to simply save  
the data in a file  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
3
Persistence and HTTP  
Recall http is a stateless protocol. It remembers nothing about  
previous transfers  
Two ways to achieve persistence:  
l
PHP cookies  
PHP sessions  
l
HTTP  
server  
Client  
Session  
Cookie  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
4
HTTP Cookies  
In internet programming, a cookie is a packet of information sent from  
the server to client, and then sent back to the server each time it is  
accessed by the client.  
Introduces state into HTTP (remember: HTTP is stateless)  
Cookies are transferred between server and client according to http.  
PHP supports http cookies  
Cookies can also be thought of as tickets used to identify clients and  
their orders  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
5
How Cookies are implemented  
l
Cookies are sent from the server to the client via “Set-  
Cookie” headers  
Set-Cookie:NAME=VALUE;expires=DATE;path=PATH;domain=DOMAIN_NAME;secure  
l
l
The NAME value is a URL-encoded name that identifies  
the cookie.  
The PATH and DOMAIN specify where the cookie  
applies  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
6
setcookie(name,value,expire,path,domain,secure)  
Parameter  
name  
Description  
(Required).Specifies the name of the cookie  
(Required).Specifies the value of the cookie  
value  
expire  
(Optional).Specifies when the cookie expires.  
e.g. time()+3600*24*30 will setthe cookie to expire in 30 days.  
If this parameter is not set, the cookie will expire at the end of the session (when the browser  
closes).  
path  
(Optional).Specifies the server path of the cookie.  
If set to "/", the cookie will be available within the entire domain.  
If set to "/phptest/", the cookie will only be available within the test directory and all sub-  
directories of phptest.  
The defaultvalue is the current directory that the cookie is being set in.  
domain  
secure  
(Optional).Specifies the domain name of the cookie.  
To make the cookie available on all subdomains ofexample.com then you'd set it to  
".example.com".  
(Optional).Specifies whether or not the cookie should only be transmitted over a secure HTTPS  
connection.  
TRUE indicates that the cookie will only be set if a secure connection exists.Defaultis FALSE.  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
7
Cookies from HTTP  
Client (e.g. Firefox)  
it026945  
GET /*.html HTTP/1.1  
Host: it026954.domain  
HTTP/1.1 200 OK  
Content-type:  
text/html  
Set-Cookie:  
name=value  
GET /*.html HTTP/1.1  
Host: it026945.domain  
Cookie: name=value  
Accept: */*  
(content of page)  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
8
Creating PHP cookies  
Cookies can be set by directly manipulating the HTTP header using  
the PHP header() function  
<?php  
header(“Set-Cookie: mycookie=myvalue; path=/; domain=.coggeshall.org”);  
?>  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
9
Creating cookies with setcookie()  
Use the PHP setcookie() function:  
Setcookie name,value,expir, path, domain, secure)  
e.g.  
<?php  
setcookie("MyCookie", $value, time()+3600*24);  
setcookie("AnotherCookie", $value, time()+3600);  
?>  
l
l
l
l
l
l
Name: name of the file  
Value: data stored in the file  
Expire: data string defining the life time  
Path: subset of URLs in a domain where it is valid  
Domain: domain for which the cookie is valid  
Secure: set to '1' to transmit in HTTPS  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
10  
Reading cookies  
To access a cookie received from a client, use the PHP  
$_COOKIE superglobal array  
<?php  
foreach ($_COOKIE as $key=>$val) {  
print $key . " => " . $val . "<br/>";  
}
?>  
Each key in the array represents a cookie - the key name is  
the cookie name.  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
11  
Creating and using cookies example  
<?php  
setcookie("MyCookie", $value, time()+7200);  
setcookie("AnotherCookie", $value, time()+7);  
?>  
<?php  
foreach ($_COOKIE as $key=>$val) {  
print $key . " => " . $val . "<br/>";  
}
?>  
l
Cookies only become visible on the next page load  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
12  
Using headers (wrong approach!)  
n
<!DOCTYPE html PUBLIC "=//W3C//DTD XHMTL 1.1//EN"  
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">  
n
n
n
<html xmlns="http://www.w3.org/1999/xhmtl" xml:lang="en">  
<head><title>PHP Script using Cookies</title>  
<meta http-equiv="Content-Type" content="text/html;  
chatset=ISO-8859-1" />  
n
n
n
n
n
n
n
n
n
</head>  
Gets an error!:  
<body>  
<?php  
Warning: Cannot modify header  
information - headers already sent by  
(output started at  
/var/www/html/TESTandre/159339/PHP/  
cookie_with_headers.php:9) in  
/var/www/html/TESTandre/159339/PHP/  
cookie_with_headers.php on line 11  
$strValue = "This is my first cookie";  
setcookie ("mycookie", $strValue);  
echo "Cookie set<br>";  
?>  
</body>  
</html>  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
13  
Using headers  
n setcookie() did not run before  
information was sent to the browser...  
n Cookies have to be sent before the  
heading elements  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
14  
Using headers (correct approach)  
n
n
n
n
n
<?php  
$strValue = "This is my first cookie";  
setcookie ("mycookie", $strValue);  
echo "Cookie set<br>";  
?>  
n
<!DOCTYPE html PUBLIC "=//W3C//DTD XHMTL 1.1//EN"  
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">  
n
n
n
n
n
n
n
n
n
n
<html xmlns="http://www.w3.org/1999/xhmtl" xml:lang="en">  
<head><title>PHP Script using Cookies</title>  
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />  
</head>  
<body>  
<?php  
echo “<p> A cookie has been set. </p>”;  
?>  
</body>  
</html>  
This is the correct approach!  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
15  
Deleting a cookie  
l
Set the cookie with its name only:  
setcookie(“mycookie”);  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
16  
Multiple data items  
l
Use explode() e.g.  
<?php  
$strAddress = $_SERVER['REMOTE_ADDR'];  
$strBrowser = $_SERVER['HTTP_USER_AGENT'];  
$strOperatingSystem = $_ENV['OS'];  
$strInfo = "$strAddress::$strBrowser::$strOperatingSystem";  
setcookie ("somecookie4",$strInfo, time()+7200);  
?>  
<?php  
$strReadCookie = $_COOKIE["somecookie4"];  
$arrListOfStrings = explode ("::", $strReadCookie);  
echo "<p>$strInfo</p>";  
echo "<p>Your IP address is: $arrListOfStrings[0] </p>";  
echo "<p>Client Browser is: $arrListOfStrings[1] </p>";  
echo "<p>Your OS is: $arrListOfStrings[2] </p>";  
?>  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
17  
Trường Đại Học B
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
b  
18  
Where is the cookie stored  
l
Depends on the browser...  
l
e.g., firefox/mozilla under /home/a________  
− Look for cookies.txt in .mozilla directory  
− Usually under:  
l
/home/a______/.mozilla/firefox/asdkfljy.default  
− Cookie is stored only if there is an expiry date  
− Otherwise it is deleted when leaving browser  
− Persistent only if an expiry date is set  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
19  
PHP Sessions  
Trường Đại Học Bách Khoa TP.HCM  
Khoa Khoa Học và Kỹ Thuật Máy Tính  
© 2017  
Lập Trình Web  
20  
Tải về để xem bản đầy đủ
pdf 36 trang myanh 24360 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Lập trình Web - Chapter 7: Cookies and Sessions - Nguyễn Hữu Hiếu", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_lap_trinh_web_chapter_7_cookies_and_sessions_nguye.pdf