PHP
  Home arrow PHP arrow Page 4 - Developing a User Personalization System with PHP and Cookies
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Developing a User Personalization System with PHP and Cookies
By: Duncan Lamb
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    1999-09-20


    Table of Contents:
  • Developing a User Personalization System with PHP and Cookies
  • Grabbing Headlines
  • User Login
  • Reading from Cookies
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Developing a User Personalization System with PHP and Cookies - Reading from Cookies
    ( Page 4 of 5 )

    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:


    setcookie(cookie name, value, expire time, string path, string domain, secure flag);

    The rules for using this function are:

    1. You must declare the cookie before the HTML header in your script, since it is sent as part of the header. That means before the <HTML> tag.
    2. You can skip entries with "", and leave off the last entries if not needed.
    3. Just using "setcookie(cookie name)" or setting the expiration time to 0 will delete the cookie off the user's system.
    4. The secure flag is a 0 or 1, and identifies whether the cookie should be transmitted over an SSL connection.
    5. Expire time is expressed in seconds, and normally defined with time()+ a number of seconds, such as time()+86400, if the cookie should expire in 24 hours.

    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.


    <? if ($visitor){ mysql_connect("localhost", "username", "pass") or DIE("Unable to connect to database"); @mysql_select_db("project") or die("Unable to select database"); $result = mysql_query("select * from users where login='$visitor'"); $row = mysql_fetch_array($result); ?> <HTML> <HEAD><TITLE>Welcome to your headlines!</TITLE> </HEAD> <BODY> <H1><CENTER>Welcome <? echo $visitor; ?>!</CENTER></H1> <br><br> The news for today is:<br> <table> <tr> <td><? include ("news/$row[news1]"); ?></td> <td><? include ("news/$row[news2]"); ?></td> <td><? include ("news/$row[news3]"); ?></td> </tr> </table> <? } else { ?> <HTML> <HEAD> <TITLE>Login now!</TITLE> </HEAD> <BODY> <br>Choose the headlines you would like to see!<br> <form method=post action="newuser.php3"> <br> Please pick a login name:<input name="login" type="TEXT" value=""> <br> Now pick a password:<input name="password" type="TEXT" value=""> <br><br> <br>News Choice 1:<SELECT NAME="news3" size=5 value=""> <option value="slashdot.lnk">Slashdot <option value="freshmeat.lnk">Freshmeat <option value="voodooextreme.lnk">VoodooExtreme <option value="devshed.lnk">DevShed <option value="bluesnews.lnk">Blue's News </select> <br><input type="submit" name="Submit" value=" Submit "> <br></FORM> <? } ?> </BODY> </HTML>

    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:


    if (isset($visitor)){

    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:


    include ("news/$row[news1]");

    (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:


    Welcome <? echo $visitor; ?>!

    For an even briefer version for the same results, PHP3 will accept the ASP standby of:


    <%= $visitor %>

    As a matter of fact, it is only lightly documented, but the <? and ?> tags are interchangeable with the ASP tags <% and %>.



     
     
    >>> More PHP Articles          >>> More By Duncan Lamb
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT