Chủ Nhật, 16 tháng 2, 2014

Tài liệu PHP and MySQL by Example- P14 pptx

,6%!)*('69<!=%!,62.!6%%8!*,!+122!setcookie()!,6+%5
>
J$%+-!*,!)%%!'&!*$%!+,,-'%!$1)!16.!0123%<!* $1*!')<!'&!'*!=1)!)%*5
E
#$%!+,,-'%!81*1!')!(%*('%0%8!&,(!*$%!3)%(!168!1))'96%8!*,!$cookie_data5!F*!')!1!)%('12'M%8!
)*('695!B%%!C'93(%!"D5D5
O
#$%!)21)$%)!1(%!)*('44%8!&(,7!*$%!)*('695!F&!.,3!8,!6,*!(%7,0%!*$%!@1+-)21)$%)<!*$%!
unserialize()!&36+*',6!,6!*$%!6%A*!2'6%!&1'2)5
D
#$%!unserialize()!&36+*',6!(%*3(6)!*$%!,('9'612!1((1.5
P
Q,3!+16!)%%!'6!*$%!0123%!,&!*$%!+,,-'%!*$%!)%('12'M%8!1((1.5
R
#$%!36)%('12'M%8!1((1.!')!4('6*%85!S%!6,=!$10%!*$%!,('9'612!0123%)!@1+-5!B%%!C'93(%!"D5D5
!
Figure 16.6. Storing an array in a single cookie.
!
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
16.3.2. Tracking Visitors with Cookies
The following examples demonstrate the use of cookies for tracking vistitor activities, such as when the visitor last
viewed the page and how many times he or she has been there, but they can also be used to check user preferences, user
IDs, and so on. Cookies are useful for retaining small amounts of information, but not all browsers support cookies and
if they are supported, a user can turn them off. To overcome these problems, a better solution is to use PHP sessions
(discussed in “What Is a Session?” on page 694 of this chapter).
Visitor Count Example
The following example uses a cookie to count the number of times the user has visited this page. Once the cookie is set,
its value will be increased by 1 each time the visitor comes back to the page.
Example 16.3.
<?php
1 $count = $_COOKIE['visits']; // Accessing the cookie value

2 if( $count == ""){
3 $count = 1; // Initialize the counter
}
else{
4 $count++;
}
5 setcookie("visits",$count); // "visits" is the cookie name
?>
<html><head><title>Setting Cookies</title></head>
<body bgcolor="lavender">
<font size=+1 face="arial">
<h2>Visitor Count with Cookies</h2>
You are visitor number <?php echo $count; ?>.<br />
</font>
</body>
</html>
Explanation
"
#$%!0123%!)*,(%8!'6!*$%!$_COOKIE!1((1.!')!%A*(1+*%8!168!1))'96%8!*,!$count5!
#$%!0123%!')!T3)*!16!'6*%9%(!*$1*!+,6*'63%)!*,!@%!'6+(%7%6*%8!@.!"!%1+$!*'7%!
*$%!3)%(!(%2,18)!*$%!419%5!F&!*$')!')!*$%!&'()*!*'7%!*$%!419%!$1)!@%%6!2,18%8<!*$%!
$_COOKIE!1((1.!='22!@%!%74*.5
:<!
>
F&!*$')!')!*$%!&'()*!*'7%!*$%!3)%(!$1)!0')'*%8!*$')!419%<!$count!='22!@%!%74*.<!168!
'*!='22!@%!)%*!*,!"5!B%%!C'93(%!"D5P5
E
C,(!%1+$!)3@)%U3%6*!0')'*!*,!*$')!419%<!* $%! 0123%!,&!*$%!+,36*%(!='22!@%!
'6+(%1)%8!@.!"5!B%%!C'93(%!"D5R5
O
#$%!setcookie()!&36+*',6!)%*)!*$%!+,,-'%!=$%6!*$%!419%!')!&'()*!2,18%85!#$%!
617%!,&!*$%!+,,-'%!')!visits!168!*$%!0123%!)*,(%8!*$%(%!='22!@%!'6+(%7%6*%8!
@.!"!%1+$!*'7%!*$%!419%!')!(%0')'*%85!#$%!+,,-'%!')!)*,(%8!,6!*$%!3)%(V)!
@(,=)%(!168!='22!@%!8%2%*%8!=$%6!*$%!@(,=)%(!')!%A'*%85!S$1*!')!'74,(*16*!*,!
6,*%!$%(%!')!*$1*!*$%!+,,-'%!')!)%6*!'6!1!$%18%(<!168!$%18%()!73)*!@%!)%6*!
@%&,(%!16.!,*$%(!,3*43*!&(,7!*$')!419%5!#$%!W#XY!,3*43*!')!421+%8!1&*%(!*$')!
2'6%!,(!ZWZ!='22!)%68!=1(6'69)!*,!*$%!)+(%%65
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 16.7. Cookies used to count visitors.

!
Figure 16.8. The cookie value is incremented each time the page is reloaded.



Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Tracking the Visitor’s Last Visit
The following example keeps track of when a visitor last viewed the page. The cookie will store the current date, which
will be retrieved the next time the page is refreshed.
Example 16.4.
J,8%!K'%=L!
(Page 1 The HTML page)

<html><head><title>Setting Cookies</title></head>
<body bgcolor="lavender">
<font size=+1 face="arial">
<h2>Tracking Visitors with Cookies</h2>
<H1>Welcome to our Site!</H1>
<p>
1 Check out our product line
<a href="http://localhost/exemples/sessions/message.php">
Click here</a>
</font>
</body>
</html>

(Page 2 The PHP Script Set a Cookie)

<?php
// Filename: "message.php"
2 $date_str="l dS \of F Y h:i:s A";
$last_visit="Your last visit was on ". date("$date_str");
3 setcookie("message","$last_visit");
?>
<html><head><title>Products</title>
</head>
<body bgcolor="lavender">
<font face="verdana" size='+1'>
<h2>Products Page</h2>
<! Rest of page goes here >
<?php
4 if(! empty($_COOKIE['message'])){ // Has the cookie been
set?
5 $when="$_COOKIE[message]";
echo $when,".< br />";
}
?>
</font></body></html>
Explanation
"
S$%6!*$%!3)%(!+2'+-)!,6!*$%!2'6-!'6!*$')!W#XY!&,(7<!$%!,(!)$%!='22!@%!8'(%+*%8!*,!*$%!
419%!G419%!:H!*$1*!+,6*1'6)!*$%!+,8%!&,(!)%**'69!1!+,,-'%5!#$%!'6'*'12!&,(7!')!)$,=6!'6!
C'93(%!"D5[5
:
I&*%(!+2'+-'69!*$%!2'6-!GC'93(%!"D5[H!'6!419%!"<!*$%!3)%(!')!8'(%+*%8!*,!419%!:<!*$%!
\Z(,83+*)!Z19%]!GC'93(%!"D5"^H5!#$%!01('1@2%!')!1))'96%8!1!)*('69!,&!1(937%6*)!*$1*!='22!
@%!)%6*!*,!*$%!ZWZ!date()!&36+*',6!,6!*$%!6%A*!2'6%<!*$%!+3((%6*!81*%!168!*'7%!,6!*$%!
)%(0%(5!G_%%4!'6!7'68!*$1*!*$%!81*%!,6!*$%!@(,=)%(!168!)%(0%(!7'9$*!6,*!@%!'6!).6+5H
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
\Z(,83+*)!Z19%]!GC'93(%!"D5"^H5!#$%!01('1@2%!')!1))'96%8!1!)*('69!,&!1(937%6*)!*$1*!='22!
@%!)%6*!*,!*$%!ZWZ!date()!&36+*',6!,6!*$%!6%A*!2'6%<!*$%!+3((%6*!81*%!168!*'7%!,6!*$%!
)%(0%(5!G_%%4!'6!7'68!*$1*!*$%!81*%!,6!*$%!@(,=)%(!168!)%(0%(!7'9$*!6,*!@%!'6!).6+5H
>
#$%!+,,-'%!')!)%*!='*$!*$%!setcookie()!&36+*',65!#$%!&'()*!1(937%6*<!"message"<!')!*$%!
617%!,&!*$%!+,,-'%!168!*$%!)%+,68!1(937%6*<!"$last_visit"<!')!*$%!0123%!*$1*!='22!@%!
)*,(%8!'6!*$%!+,,-'%5
E
#$%!&'()*!*'7%!*$')!419%!')!1++%))%8!*$%!+,,-'%!')!)%*5!F*)!0123%!='22!6,*!@%!101'21@2%!36*'2!
*$%!6%A*!*'7%!*$%!419%!')!0'%=%85!F&!*$%!+,,-'%!$1)!1!0123%!G'5%5<!')!6,*!%74*.H<!*$%!
7%))19%!='22!+,6*1'6!*$%!81*%!)*('69!*$1*!=1)!1))'96%8!*,!*$%!+,,-'%!@.!*$%!setcookie()!
&36+*',6!'6!*$%!4(%0',3)!0'%='69!,&!*$%!419%5
O
#$%!0123%!,&!*$%!+,,-'%!')!%A*(1+*%85!F*!')!*$%!81*%!)*('69!*$1*!=1)!1))'96%8!*,!*$%!+,,-'%!
*$%!21)*!*'7%!*$%!0')'*,(!0'%=%8!*$')!419%5!`0%(.!*'7%!*$%!0')'*,(!(%&(%)$%)!*$')!419%<!*$%!
0123%!,&!*$%!+,,-'%!='22!@%!*$%!+,,-'%!0123%!*$1*!=1)!)%*!,6!$')!,(!$%(!21)*!0')'*<!*$1*!')<!
*$%!81*%!168!*'7%!,&!*$%!21)*!0')'*5
!
Figure 16.9. The HTML initial form (page 1).
!
!








Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 16.10. After returning to this page, the cookie value is displayed.
!

16.3.3. Extending the Life of a Cookie
How long will a cookie stay in the cookie jar? Normally a cookie expires when the browser is exited. However, the
cookie’s life span can be controlled by setting the expiration date in the cookie’s expire attribute, the third argument
in PHP’s setcookie() function. The time the cookie expires is represented as a UNIX timestamp; that is, the
number of seconds since January 1, 1970, 00:00:00 GMT, known as the epoch. The time() function will give you the
current time in seconds, and by adding additional seconds, you can set the expiration date of a cookie to some time in
the future. By subtracting from this value, the time will be past time, which will cause the cookie to be deleted. The
time returned is expressed in GMT time, the required format for the expire attribute.
To get the time, two PHP functions are provided: time() and mktime().
The time() Function
The time() function returns the current time in UNIX time (UNIX timestamp). By adding the number of seconds to
the output of the time() function, you can set the amount of time from now until some future time when the cookie is
to expire.
Table 16.1. Units of Time in Seconds
Unit%of%Time
Seconds
X'63*%
60
W,3(
60 * 60
a1.
60 * 60 * 24
S%%-
60 * 60 * 24 * 7
X,6*$
60 * 60 * 24 * 30
!




Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Format
int time ( void )
!
Example:
$nextWeek = time() + (60 * 60 * 24 * 7); (60 seconds * 60 minutes *
24 hours * 7 days)

Example 16.5.
<?php
$date_str="l dS \of F Y h:i:s A";
$last_visit="Your last visit was on ". date("$date_str");
1 $expire=60*60*24*30 + time(); // One month
2 setcookie("message","$last_visit", $expire);
?>
Explanation
"
#$%!01('1@2%!')!1))'96%8!*$%!0123%!,&!,6%!7,6*$<!>^!81.)<!&(,7!6,=!'6!
7'22')%+,68)5
:
#$%!setcookie()!&36+*',6!')!617%8!message<!'*!+,6*1'6)!*$%!81*%!,&!*$%!21)*!
0')'*<!168!'*!='22!%A4'(%!'6!,6%!7,6*$5!#$%!expire!0123%!')!+12+321*%8!@.!188'69!
*$%!637@%(!,&!)%+,68)!'6!1!7,6*$!*,!*$%!+3((%6*!*'7%!Gtime()H5!I&*%(!,6%!
7,6*$<!'&!*$%!0')'*,(!(%*3(6)<!*$%!+,,-'%!='22!@%!(%)%*5

The mktime() Function
The mktime() function will also get the UNIX time. It has a different format. Arguments can be set to 0 (zero) from
left to right if you want to use the default values. However, you can leave out arguments on the right side to get the
defaults. (The year is either two or four digits.)
Format
int mktime ( [int hour [, int minute [, int second [, int month [,
int day [, int year [, int is_dst]]]]]]] )
!
Example:
$lastday = mktime(0, 0, 0, 6, 0, 2006); // Last day of May echo
date("M-d-Y", mktime(0, 0, 0, 1, 1, 2006)); // "Jan-01-2006"

16.3.4. Buffering and HTTP Headers
Because cookies are sent in an HTTP header, you cannot execute any other output before sending the header or you will
get a PHP warning. In the following example, the fact that there is a blank line at the top of the file caused the warning.
The cookie headers must be set first unless you turn on buffering.
Example 16.6.
< this blank line caused a warning !!!
<?php
setcookie("usr","Ellie Quigley"); // Headers must be sent
first
setcookie("color","blue");
?>
<html>
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
<head><title>The Cookie Array?</title></head>
<body bgcolor="lavender">
< Code continues here >
</body>
</html>
Explanation
The header information must be sent first, or a warning is issued, as in Figure 16.11. Even a blank line will cause a
warning.
Figure 16.11. Header information should be sent first!
!

If you need to precede any HTTP headers (not just cookie headers) with other output, PHP provides a set of buffering
functions that allow you to save all the script’s output in a buffer until the script ends (starting with PHP 4.0). When the
script ends, first the HTTP headers, and then the contents of the output buffer, are sent to the browser.
The functions that help you control output buffering are shown in Table 16.2.




Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Table 16.2. Buffering Functions
Function
What%It%Does
ob_start()
`61@2%)!,3*43*!@3&&%('695!;,!,3*43*!')!)%6*!&(,7!*$%!)+('4*!G,*$%(!*$16!
$%18%()H5!F*!')!)10%8!'6!16!'6*%(612!@3&&%(5
ob_end_flush()
C23)$%)!*$%!,3*43*!@3&&%(<!168!8')1@2 %)! ,3*43*!@3&&%('695
ob_end_clean()
J2%16)!*$%!,3*43*!@3&&%(!='*$,3*!)%6 8'69!'*<!168!8')1@2%)!,3*43*!
@3&&%('695
ob_get_clean()
b%*3(6)!*$%!+,6*%6*)!,&!*$%!,3*43*!@3&&%(!168!%68)!,3*43*!@3&&%('69
ob_get_length()
b%*3(6)!*$%!2%69*$!,&!*$%!,3*43*!@3&&%(5
ob_get_contents()
b%*3(6)!*$%!+3((%6*!,3*43*!@3&&%(!1)!1! )*('695!#$')!122,=)!.,3!*,!4(,+%))!
=$1*%0%(!,3*43*!*$%!)+('4*!%7'**%85
ob_gzhandler()
I!+122@1+-!&36+*',6!&,(!ob_start()5!c)%&32!&,(!)%68'69!+,74(%))%8!81*15
!
The ob_start() and ob_end_flush() Functions
The ob_start() function enables output buffering and the ob_end_flush() function flushes out the buffers and
then turns buffering off. When your script ends, PHP will automatically flush the buffers, so you can omit
ob_end_flush(). It is possible to call ob_start() multiple times; and if so, you would have to call
ob_end_flush() for each level.
Format
bool ob_start ( [callback output_callback [, int chunk_size [,
bool erase]]] ) bool ob_end_flush ( void )
!
Example:
ob_start(); ob_end_flush();

Example 16.7.
J,8%!K'%=L!!
<?php
1 ob_start(); // Turn on output buffering
?>

<html><head><title>The Cookie Array?</title>
</head>
<body bgcolor="lavender">
<font face="verdana" size='+1'>
<h2>$_COOKIE[]</h2>

<?php
2 setcookie("usr","Ellie Quigley");

setcookie("color","blue");
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
?>

<?php
if(! empty($_COOKIE[color])){
echo "<pre>";
print_r($_COOKIE);
echo "</pre>";
}

?>

</font>
</body>
</html>
<?php
3 ob_end_flush(); // Flush the buffer and end output
buffering
?>
Explanation
"
#$%!ob_start()!&36+*',6!*3(6)!,6!,3*43*!@3&&%('695!;,=!,62.!W##Z!$%18%()!
='22!@%!)%6*!168!*$%!(%)*!,&!*$%!4(,9(17V)!,3*43*!='22!@%!)10%8!36*'2!*$%!
4(,9(17!%68)<!1*!=$'+$!*'7%!'*!='22!@%!)%6*5
:
#$%!setcookie()!&36+*',6!+16!@%!421+%8!@%2,=!*$%!,*$%(!,343*!='*$,3*!
+13)'69!=1(6'69)5!#$')!,3*43*!='22!@%!)%6*!&'()*!83%!*,!*$%!@3&&%('69!)%*!34!,6!
2'6%!"5
>
#$%!ob_end_flush()!&36+*',6!')!6,*!6%+%))1(.<!@3*!')!3)%8!$%(%!*,!&23)$!,3*!*$%!
@3&&%()!168!%68!*$%!,3*43*!@3&&%('69!&,(!*$')!)%))',65

Output Buffering and php.ini
If you want buffering set for all your PHP scripts, you can enable the php.ini directive output_buffering. If
you do, every PHP script will behave as if it begins with a call to ob_start().
From the php.ini file:
!
J,8%!K'%=L!
; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit. You can enable output buffering during runtime by calling the output
; buffering functions. You can also enable output buffering for all files by
; setting this directive to On. If you wish to limit the size of the buffer
; to a certain size -you can use a maximum number of bytes instead of 'On', as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = Off

!
Output buffering is turned off by default. If you want to turn it on for all scripts, go to the php.ini initialization file
and change the output_buffering directive to “On”.

Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.

Không có nhận xét nào:

Đăng nhận xét