1. Trang chủ
  2. » Mẫu Slide

Slide môn học PHP session 7 working with cookies

24 246 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 24
Dung lượng 198 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

What are Cookies? Websites use cookies to store user-specific information  Cookies are stored on the hard disk of the Web browser  User information and preferences are send between We

Trang 1

Working with Cookies

Session 16

Trang 2

 Database APIs allows the developers

to write applications that are

easily accessible between the

database products

accessing data from the database

server

Review - I

Trang 5

What are Cookies?

 Websites use cookies to store

user-specific information

 Cookies are stored on the hard

disk of the Web browser

 User information and preferences

are send between Web browser and

Web server

Trang 6

Static Web Pages

 When a Web browser requests for a static Web page, the server responds by sending the Web page the browser requested for

 No user interaction

Trang 7

Dynamic Web Pages

and record it for further processing

Trang 8

Types of Cookies

Persistent - Remains stored in

the Web browser for a period

specified during the time of it’s

creation

Non-persistent - Deleted from the

Web browser as soon as the user

closes the browser

Trang 9

Common Uses of Cookies

 Enable websites determine:

 Number of times the user has visited the website

 Number of new visitors

 Number of regular users

 Frequency of a visitor visiting the website

 Store the date that a user last visited the website

 Maintain a customized Web page settings for a user

Trang 10

Working with Cookies

 Web servers and Web browsers send

each other cookies in HTTP

headers

 Web server sends the cookie in a

Set-Cookie header field

 Set-Cookie is a part of HTTP

response

Trang 11

Setting a Cookie

 Refers to sending the cookie to the browser

 PHP uses two functions to set a

cookie, such as:

 setcookie(): Sends cookie with urlencoding

 setrawcookie(): Sends cookie without

urlencoding

Trang 12

setcookie() Function

 Generates a correct header field

that is sent along with the rest of the header information

 Syntax:

setcookie(name, value, expire, path, domain, secure)

Trang 13

Example of setcookie() Function

setcookie("mycookie", $mapname,

time()+86400, "/webmap/", ".webworldmaps.com");

Where,

 mycookie - Name of the cookies

 time()+3600 - Time when the cookie will expire

 /webmap – Path where the cookie will be stored

Trang 14

Retrieving Cookie Value

 PHP provides three ways of

retrieving a cookie value:

 Passing a variable as cookie name

 Using $_COOKIE[]

 Using $HTTP_COOKIE_VARS[]

Trang 15

Passing Variable as Cookie Name

 Use the variable as the cookie

name

 Syntax:

echo $cookie_name

 PHP searches all the variables

present in the client computer

 Works only when the

Trang 16

Using $_COOKIE[]

 Requires PHP 4.1

 Simpler and more secured than

using the $HTTP_COOKIE_VARS[]

 Syntax:

$_COOKIE[‘$cookie_name’]

Trang 17

Using $HTTP_COOKIE_VARS[]

 Global variable that reads a value

of the cookie

 PHP passes the name of the variable

as the key to the

$HTTP_COOKIE_VARS[] array

 Syntax:

$HTTP_COOKIE_VARS[$cookie_name];

Trang 18

Deleting a Cookie - I

 Similar to setting a cookie

 Two ways to delete a cookie:

 Resetting the expiry time of the cookie to a

time in the past

 Resetting the cookie by specifying the name of

the cookie

Trang 19

Deleting a Cookie - II

 Example to delete a cookie with a

date in the past:

setcookie ("$cookie_name", "",

time()-8000);

 Example to delete Cookie by

specifying the name of the cookie:

setcookie($cookie_name);

Trang 20

Drawbacks of Cookies - I

 Cookies are not considered secured and reliable

 Some of the drawbacks of cookies:

 Cookies cannot contain more than a certain amount of

Trang 21

Drawbacks of Cookies - II

 Some users disable cookies while accessing

websites

 There can be multiple persons using the same

computer visiting the same website

 Cookies need to be called on each Web page

Trang 22

Summary - I

user-specific information

browser’s hard disks

send to and from the Web browser

 Persistent

Trang 23

Summary - II

 PHP provides three ways of

retrieving a cookie value:

 Passing a variable as cookie name

 Using $_COOKIE[]

 Using $HTTP_COOKIE_VARS[]

 PHP uses two functions to set a

cookie, such as

Trang 24

Summary - III

 Two ways to delete a cookie:

 Drawbacks of cookies:

Ngày đăng: 30/11/2016, 22:11

TỪ KHÓA LIÊN QUAN