Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,88 MB
Nội dung
}
?>
</font>
</body>
</html>
Explanation
"
#$%!&'()*!+,,-'%!-%./0123%!41'(!')!)%*5!#$%!617%!,&!*$%!+,,-'%!')!"usr"!168!*$%!
+,((%)4,68'69!0123%!')!"Ellie Quigley"5
:
#$%!)%+,68!+,,-'%!-%./0123%!41'(!')!)%*5!#$%!617%!,&!*$%!+,,-'%!')!"color"!168!*$%!
+,((%)4,68'69!0123%!')!"blue"5!;,(7122.<!*$%!3)%(!=,328!4(,0'8%!*$%!0123%!&(,7!1!
&,(75
>
?%+13)%!+,,-'%)!='22!6,*!@%+,7%!0')'@2%!36*'2!*$%!6%A*!2,18'69!,&!*$%!419%!=$%(%!*$%!
+,,-'%!)$,328!@%!0')'@2%<!.,3!+16!*%)*!'&!1!+,,-'%!=1)!)3++%))&322.!)%*!@%&,(%!%A*(1+*'69!
'*)!+,6*%6*)5!B%%!C'93(%!"D5>5
E
#$%!print_r!&36+*',6!8')421.)!*$%!+,6*%6*)!,&!*$%!+,,-'%5!F&!*$%!+,,-'%!$18!6,*!@%%6!
)%*!,(!$18!%A4'(%8!*$%(%!=,328!@%!6,!,3*43*!G)%%!C'93(%!"D5:H5!I22!*$%!,*$%(!1**('@3*%)!
)%*!&,(!*$%!+,,-'%<!2'-%!%A4'(1*',6!81*%<!41* $<! )%+3('*.<!168!),!,6<!1(%!6,*!0')'@2%5
!
Figure 16.2. The first time the page is viewed the $_COOKIE array is empty.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 16.3. When the page is refreshed, the $_COOKIE array has cookie values.
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Figure 16.4. The browser sends the cookie back to the server; the server sets the cookie in a header. See Figure
16.5, a diagram illustrating server/browser/PHP interaction with cookies.
!
!
Figure 16.5. The cookie is sent in an HTTP header.
!
!
!
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Storing Multiple Values in One Cookie—Serialization
The setcookie() function accepts one string as its value. In the previous example, the setcookie() function
was called twice to register two cookie values. Because the number of cookies is limited to 20 per domain, you might
want to assign multiple values to one cookie, for example, data coming in from a form. In the following example, one
cookie will store three values. This example demonstrates how to serialize data. Serializing the data allows you to
convert an array into a string that will be accepted by the cookie. After retrieving the cookie contents, you will have to
unserialize it to convert the string back to an array.
The PHP serialize() function returns a string containing a byte-stream representation of the value, making the
value acceptable for storage anywhere—in this example, a cookie, though serialization is also used for storing variables
and objects in a file or database. (If you go to your browser and look at the actual data stored in the cookie, it has been
URL-encoded.)
Use unserialize() to return the string to its orginal form.
Example 16.2.
J,8%!K'%=L!
<?php
1 $info = array("ellie", "yellow", 22);
2 setcookie("usr", serialize($info));
?>
<html><head><title>Multiple Cookie Values</title></head>
<html><head><title>The Cookie Array?</title></head>
<body bgcolor="lavender">
<font face="verdana" size='+1'>
<h2>$_COOKIE[]</h2>
<pre>
<b>
<?php
3 if(! empty($_COOKIE['usr'])){
4 $cookie_data= $_COOKIE['usr'];
5 $cookie_data=stripslashes($cookie_data);
6 $cookie_data=unserialize("$cookie_data");
echo "What's in the cookie array< br />";
7 print_r($_COOKIE);
echo "<pre>Unserialized data< br />";
8 print_r( $cookie_data);
}
?>
</b>
</pre>
</font>
</body>
Explanation
"
#$%!1((1.!')!1))'96%8!1!2')*!,&!0123%)5
:
#$%!setcookie()!&36+*',6!')!9'0%6!*$%!617%!,&!*$%!+,,-'%!&,22,=%8!@.!*$%!0123%5!#$%!
0123%!')!16!1((1.!*$1*!')!)%('12'M%8!'6*,!,6%!)*('695!#$%!6%=!)*('69!='22!@%!'6!1!&,(71*!*$1*!
')!1++%4*1@2%!&,(!16.!*.4%!,&!)*,(19%5!F*!(%4(%)%6*)!*$%!81*1!*.4%!168!637@%(!,&!
+$1(1+*%()!'6!*$%!,('9'612!81*15!a:3!7%16)!1!*$(%%N%2%7%6*!1((1.<!s:5!1!ON+$1(1+*%(!
)*('69<!168!),!,6<!1)!)$,=6!'6!*$%!,3*43*!,&!*$')!4(,9(175!?.!)%('12'M'69!*$%!1((1.!'6*,!
,6%!)*('69<!=%!,62.!6%%8!*,!+122!setcookie()!,6+%5
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
,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, andby 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.
[...]... the php. ini file and is up to the server setting to manage it Also, if the user restarts the computer or the Web browser, his or her session ID might be lost and the next visit to these pages will create a brand-new session Remembering Users and Preferences over Multiple Pages The following example consists of three pages: the HTML form, a file that handles the form data and starts a session, and links... necessary, but is used here to flush out the buffers and end the output buffering for this session 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: Code View: ; Output buffering allows you... the same way A PHP session, like a cookie, is a way for the PHP to keep track of that Web site visitor even after he or she leaves or logs off A visitor makes a request from his or her browser to retrieve a Web page as follows: http://server/homepage .php The server program, in this example, homepage .php, is a PHP program PHP starts a session and sends a unique session ID number, similar to the claim... process whatever output the script emitted ob_gzhandler() A callback function for ob_start() Useful for sending compressed data 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... course, PHP will need read and write access to the new path to retrieve and save session data Format string session_save_path ( [string path] ) Example: session_save_path("/newpath"); echo session_save_path(); Example 16.9 < ?php 1 echo "Your session files are stored in " session_save_path() ".< br />"; Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 2 3 if ($handle... (false !== ($file = readdir($handle))) { echo "$file< br />\n"; } echo ""; closedir($handle); } ?> Explanation 1 The session_save_path() function returns the path location where the session files are stored 2 The opendir() function opens the directory folder where the session data is stored and returns a handle to that directory, $handle 3 The readdir()... 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 16.3.5 Deleting a Cookie When cookies are created, they are, by default, deleted when the user closes his or her browser You have seen how to expand the life of a cookie,... enter the $book section.< br />"; $section=$book "_page .php" ; // Creating a variable ?> To browse the < ?php echo $book; ?> Section: Click here < br /> Explanation 1 If the user has filled out the form properly both the 'book' and 'user' fields will have values and the statements in the if block will be executed 2... page uses the new name) < ?php 5 session_name('ColorSite'); 6 session_start(); $favorite_color = $_SESSION['favorite_color']; print_r($_SESSION); ?> Your Favorite Color Your favorite color is < ?php echo $favorite_color; ?> < ?php ob_end_flush(); // Flush the buffer and end output buffering ?>... ID is passed in a cookie Figure 16.14 The cookie file and the session file have the session ID in common Although cookies are the default way to pass the session ID back and forth between browser and server, you can also pass the session ID as GET or POST data in the same way as when submitting a form Recall that GET data is URLencoded and attached with a ? to the URL, whereas the POST data . 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. follows:
http://server/homepage .php
!
The server program, in this example, homepage .php, is a PHP program. PHP starts a session and sends a unique
session