picture of Jim King
Isn't it great to go to a web site and it appears as though it was designed just for you. This article will help you design a web site which makes visitors feel like it was designed just for them.
First, lets scope out the project. You will need a mysql database, a table to store the visitor's information, and php3 or php4 (I will explain how to use features of both). This application will gather information about a visitor so the experience will become more and more tailored to the visitor over time. For the sake of this article, we will limit the information collected to that of the visitor's state. This application is flexible enough that you can collect any information you like. First, lets create the MySQL table (we will assume that you have already created a database named leads):
create table leads(
sid char(32) NOT NULL,
state char(2));
For the rest of the article, we will assume that the database and the data table are named leads, and that the cookie identifying the visitor is named lead.
Now that we have database setup, we are ready to get started. Lets review the class file that will be used to generate what I call a lead. This class sets a cookie to identify the visitor on subsequent occasions and then stores all of the information that we gather into the leads table, indexed by the cookie. To use this class, just add the following code to the top of any page that you would like to customize:

<?php

mylead 
= new lead("dbhost""database""dbusername""dbpassword""leadstablename");

if(!isset(
$HTTP_COOKIE_VARS[lead])) {
    
$sid $mylead->setCookie();
} else {
    
$thelead $mylead->preloadInfo$HTTP_COOKIE_VARS[lead] );
}

?>
What this code does is:
Line 1: Instantiate the class.
Line 3: Check to see if the cookie has already been set. If it has been set, skip to line 10. If not, set the cookie.
Line 9: Pre-load all the information that we have on the lead into the $thelead array. This array will look exactly like the database table.
If you have defined the columns email, state, and zip in the database, then you can access that information with the following code:

<?php

$email 
$thelead[email];
$state $thelead[state];
$zip $thelead[zip];

?>
Since the database will contain no information on a visitor (or "lead") if the cookie has not been set, that is where things end. If the cookie does exist, then we retrieve all the information that we have on the visitor and store it to an array named $thelead. In our example, the only column in the leads table is state. Therefore, the $thelead array should only contain the element state. Now, there are a couple things to note at this point. If you are using php3, you will have to retrieve this information from the database for every page you intend on using it for. In php4, we can use sessions and set a session variable named $thelead. Then, the $thelead array will be available on each page the user visits. In the examples file, I have provided an example for both php3 and php4.
Using this information to customize the page is now easy. For example, let's say that I have a database of ads. I only want the ads specific to the state of Wisconsin to be shown to visitors from Wisconsin. So I write a short if statement that says if the visitor is from Wisconsin, then display the Wisconsin ads. Like this:

<?php

if( $thelead[state] == WI) {
    
//code to display Wisconsin ads
} else {
    
//code to display random generic ads
}

?>
Now that we know how to use the collected information, let's see how to collect the information.
Collecting information on visitors is accomplished utilizing the various forms on a web site. For example, a web site that sells products online could use this application to grab the state from a visitor during the check out procedure or when a visitor subscribes to an email newsletter. The point is, information is only gathered from an existing form. This is done by using the addItem() method of the lead class. The addItem() method takes as arguments the database column to store the data being collected (in our case state), the value of the data being collected (in our case the visitors two digit state abbreviation), and finally the value of the cookie that identifies the visitor.
Again, you must instantiate the class, verify the cookie is set and then add the data to the database.

<?php

mylead 
= new lead("dbhost""database""dbusername""dbpassword""leadstablename");

if(!isset(
$HTTP_COOKIE_VARS[lead])) {
    
$sid $mylead->setCookie();
    
$result $mylead->additem("state"$state$sid);
} else {
    
$result $mylead->additem("state"$state$HTTP_COOKIE_VARS[lead]);
}

?>
That's all there is to it. The nice thing about this application is that you can define what information you would like to collect and it is saved and retrieved from the database using the same class methods each time. One thing I did not go over was error handling. Error handling is accomplished by using the error class internal to the leads class. The additem() method will return an object with all the error information in it. The error information is $result->errno, the error number 1 for success and 0 for failure, $result->errstr, an explanation of the error, and $result->method, the method that the error occurred in.
Customizing a visitor's experience is increasingly more important to keep the visitors attention and generate sales. I hope to find the time to expand this article in the future.
-- Jim