PHPBuilder - Tracking User Activity in PHP with Cookies and Sessions

RSS Twitter

Tracking User Activity in PHP with Cookies and Sessions

by: Leidago Noabeb
May 24, 2011

When you have more than one page in your PHP-based website and want to keep track of or personalize user activities, or when your application grows to more than just an information board, then it is time to maintain state in the site. The main purpose of maintaining state in PHP applications is to keep track of users and their activities. It can also be used to limit a user's ability to access certain components of a website.
Without maintaining state, an e-commerce site would not be able to support shopping on the Internet, because the shopping cart needs to "remember" the products you selected to buy and it needs to maintain state for that.
In this article we will look at what maintaining state in PHP applications entails. Specifically, PHP offers two methods for tracking data. The first is cookies and the second is called sessions. We will be looking at these methods in detail.

Using Cookies in PHP to Maintain State

What are "cookies"? Cookies provide a way for a server to store information about a user on the user's machine. The purpose for this is mainly for the server to remember who you are every time you visit a particular site. Think of it as a name tag or identifier that the server assigns to you.
On a more technical level, cookies are basically text files stored by websites on a computer that accesses websites. Each cookie can contain around 4,000 characters, and up to 20 cookies can be stored for each website. The client can store a maximum of 300 cookies. As you might expect, the text in cookies are stored in plain text, so if you are going to store sensitive data, you will have to encrypt it to be more secure.
To create a cookie in PHP, the setcookie() function must be used at the top of your PHP script. No white spaces or any kind of text should precede the calling of the function. In the example below, we use a form to collect the name and age of a user and then store that information in a cookie:
   $err = "";
   echo "dghsdgh";
   //check if the form values are not empty
   $err = "Please enter  a name.";
   $err .= "Please enter  a age.";
   if(strlen($err) < 1 ){
setcookie('User', $_POST['txtname']);
echo "Cookies set";   
echo "The following errors occurred: ".$err;

<html xmlns="">
<title>Handling Cookies in PHP</title>
<style type="text/css">
.style1 {font-size: 14px}

<form id="form1" name="form1" method="post" action="setcookie.php">
    <td colspan="2"><h1>Set Cookie Form </h1></td>
    <td width="19%">Name</td>
    <td width="81%"><label>
      <input name="txtname" type="text" id="txtname" />
      <input name="txtage" type="text" id="txtage" />
    <td> </td>
To view the cookie data, we can simply open up the cookie file on the system using any text editor or we simply write a routine similar to how you would retrieve a form value. For example, if you've set a cookie value like this:
Then you would retrieve it like this:
echo $_COOKIE('Username');

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Leidago Noabeb



(Maximum characters: 1200). You have characters left.