05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

A third technique for maintaining state is to use cookies. A cookie is a bit of information<br />

that the server can give to a client. On every subsequent request the client will<br />

give that information back to the server, thus identifying itself. Cookies are useful for<br />

retaining information through repeated visits by a browser, but they’re not without<br />

their own problems. The main problem is that some browsers don’t support cookies,<br />

and even with browsers that do, the user can disable cookies. So any application<br />

that uses cookies for state maintenance needs to use another technique as a fallback<br />

mechanism. We’ll discuss cookies in more detail shortly.<br />

The best way to maintain state with <strong>PHP</strong> is to use the built-in session-tracking system.<br />

This system lets you create persistent variables that are accessible from different<br />

pages of your application, as well as in different visits to the site by the same user.<br />

Behind the scenes, <strong>PHP</strong>’s session-tracking mechanism uses cookies (or URLs) to elegantly<br />

solve most problems that require state, taking care of all the details for you.<br />

We’ll cover <strong>PHP</strong>’s session-tracking system in detail later in this chapter.<br />

Cookies<br />

A cookie is basically a string that contains several fields. A server can send one or<br />

more cookies to a browser in the headers of a response. Some of the cookie’s fields<br />

indicate the pages for which the browser should send the cookie as part of the<br />

request. The value field of the cookie is the payload—servers can store any data<br />

they like there (within limits), such as a unique code identifying the user, preferences,<br />

etc.<br />

Use the setcookie() function to send a cookie to the browser:<br />

setcookie(name [, value [, expire [, path [, domain [, secure ]]]]]);<br />

This function creates the cookie string from the given arguments and creates a<br />

Cookie header with that string as its value. Because cookies are sent as headers in the<br />

response, setcookie() must be called before any of the body of the document is sent.<br />

The parameters of setcookie() are:<br />

name<br />

A unique name for a particular cookie. You can have multiple cookies with different<br />

names and attributes. The name must not contain whitespace or semicolons.<br />

value<br />

The arbitrary string value attached to this cookie. The original Netscape specification<br />

limited the total size of a cookie (including name, expiration date, and<br />

other information) to 4 KB, so while there’s no specific limit on the size of a<br />

cookie value, it probably can’t be much larger than 3.5 KB.<br />

expire<br />

The expiration date for this cookie. If no expiration date is specified, the<br />

browser saves the cookie in memory and not on disk. When the browser exits,<br />

the cookie disappears. The expiration date is specified as the number of seconds<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Maintaining State | 179

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!