Cookies are an easy, transparent way to store data on a user's computer, and to retrieve that data every time the user returns. Used wisely so they don't store sensitive data, they can allow seamless interactivity for any web application. To create a cookie on a user's computer, use the following syntax in PHP3:
The rules for using this function are:
Values saved in cookies by a particular site are automatically sent to that site in every header. PHP3 automatically decodes the variable and assigns it to a variable, so to access its value in PHP3, just use the variable as you would normally -- it already exists. For example, we can grab the cookie and print out the customized page we have been building for this user. The following script will grab the appropriate row from the database, and print out the page with all of their settings.
This script branches between two different possibilities: the first section is executed if the user has a cookie set, at which time their persoanl settings are retrieved and used to build a page, while the last part of the script is executed when no cookie is set (this is the same form used on the last page). By making this our index file, users will automatically get the page intended for them. PHP3 does all the dirty work, automatically reading the headers sent from the client and placing the cookie in a variable. Note how the $visitor variable will have a value and be true if the cookie has been set. Another way to test for a cookie is with the "isset" command, as in:
PHP3 does all the dirty work, automatically reading the headers sent from the client and placing the cookie in a variable. Nothing could be easier! If a cookie has been set, the script reads its value, grabs the appropriate row from the database, and includes the appropriate headlines. Since values stored for news choices are named the same as the filenames they are stored in, putting them into the page is as easy as:
(Remember, we put those headlines in the "news/" subdirectory.) The only other bit of code in this script is where the user's login name is printed out in a welcome message:
For an even briefer version for the same results, PHP3 will accept the ASP standby of:
As a matter of fact, it is only lightly documented, but the <? and ?> tags are interchangeable with the ASP tags <% and %>.
blog comments powered by Disqus |