What is a Session Variable? Session variables are similar to cookies, that is they are used to store information for a particular period of time. The values in session variables exist only till the session exists. They are used to carry information from one page to another of a web site. We can create a session using a session identifier and store it in the server. When the client makes any request, the data stored in the session variable can be accessed by PHP any number of times until the session is ended. Why do we need a Session Variable? In web sites, passing information between pages using a query string is very difficult. For example once you log into a site, passing the username to all the pages of the site using query string is very difficult, but this can be done easily using Session variables.
These variables can be used to pass information from one page to another without using a query string, since it is easy to maintain and retrieve. How to set Session Variables Session variables can set using 'session_register' function in PHP as shown below: Syntax: session_register("string"); // string - mention the name of the session variable Example: session_register("UserName"); UserName - name of the session variable Destroy a Session Variable They can be destroyed using 'session_destroy' function. This is used to end a session when the user logs out. The code for destroying all the session variables stored in the server is given below: Syntax: session_destroy(); Example: session_destroy(); Destroy a Session variable In the above code it will destroy/delete all the session variables that are stored in the server. In some cases we may want to delete only a particular session variable, this can be done by specifying its name as shown below: Syntax session_unregister("string"); //string - name of the session variable Example: session_unregister("UserName"); UserName - name of the session variable
Building A Persistent Shopping Cart With PHP and MySQL (Page 1 of 6 )
If you take a look around any PHP resource site, you'll notice an exorbitant amount of shopping cart scripts. The fact of the matter is that shopping cart scripts aren't hard to develop. In this article Mitchell shows us how to build a simple shopping cart that persists across multiple browser sessions. He uses PHP, MySQL, and JavaScript to implement each part of the shopping cart. If you take a look around any PHP resource site, you'll notice an exorbitant amount of shopping cart scripts. The fact of the matter is that shopping cart scripts aren't hard to develop. A shopping cart script simply needs to work with some sort of storage device to store a list of items and how many of those items your visitor has chosen to add to their cart.
Typically we would use a database (such as MySQL) to store a list of products and their prices. We would then use PHP's session handling capabilities to store a list of products and amounts relating to each particular visitor, based on their session details.
This is a good way to do things, but what happens if you want to persist the details of that users cart across multiple sessions? I.e., if they close the web browser and come back the next day. You could setup a login system where users register and confirm their details by replying to a confirmation email, but more often you'd like to keep your checkout as simple as possible by just letting the user enter their address and payment method and continue right through to the confirmation page.
By persisting a cookie on the users machine and by using some PHP, we can make a simple shopping cart that will store the details of what the user wants to buy. These details will remain intact, even after the user closes the browser.
So, in this article we will build a simple persistent shopping cart. We will be using PHP and MySQL to do so, so y ou should have intermediate knowledge of both PHP and MySQL. You will be able to take the code that we will use and adapt it to create your own simple shopping cart for your web site.
Building A Persistent Shopping Cart With PHP and MySQL - Creating the database (Page 2 of 6 )
Let's assume that we're running a web site that sells Sony Playstation 2 games. We need one table to store the details of each product, and we also need a table to store the contents of each users shopping cart, so that they can be persisted over multiple sessions.
Fire up the MySQL console application and create a database named cart. Fill it with two tables: items and cart, like this:
create database cart;
create table items ( itemId int auto_increment not null, itemName varchar(50), itemDesc varchar(250), itemPrice decimal(4,2), primary key(itemId), unique id(itemId) );
create table cart ( cartId int auto_increment not null, cookieId varchar(50), itemId int, qty int, primary key(cartId), unique id(cartId) );
The first table, items, will contain a list of items that the user will be able to add to his/her cart. The items table contains four fields, as described below: ᆵᄒᄒᄒᄋ itemId: A unique numeric identifier that gives each item its own ID. ᆵᄒᄒᄒᄋ itemName: The name of the item in the catalog. ᆵᄒᄒᄒᄋ itemDesc: A short description of the item in the catalog. ᆵᄒᄒᄒᄋ itemPrice: The price of the item, such as 45.99. The cart table will store the details of each item in the users cart as he/she adds them. The cart table also contains four fields, which are described below: ᆵᄒᄒᄒᄋ cartId: A unique numeric identifier that gives each item in the users cart its own ID. ᆵᄒᄒᄒᄋ cookieId: This is the most important field in both of the tables. It's used to persist the users cart over multiple sessions. It is the value of the session ID with which the user first started browsing our product range. ᆵᄒᄒᄒᄋ itemId: The ID of the item that the user is purchasing. ᆵᄒᄒᄒᄋ qty: The number of this specific item being purchased. In this article I won't show you how to create forms to add, edit and delete items from the database. Thats out of the scope of this article. We will manually add some products (Playstation 2 games) to the items table now:
insert into items values(0, 'Tony Hawk 3', 'Tony Hawk is back. Join him in this popular skating game where speed, collisions and tricks come together to produce the best skating game of all time!', 23.95);
insert into items values(0, 'FIFA Soccer 2002', 'The FIFA range of soccer games are the most popular in their genre. FIFA Soccer 2002 includes an all new team line up, advanced management capabilities, and richer, more realistic graphics.',36.50);
insert into items values(0, 'SSX Tricky', 'Image snowboarding down a steep hill at 100 miles per hour and you have SSX Tricky. Its packed with new players, new moves, and a whole new list of stages to complete.', 45.50);
Now that our database is good and ready to go, lets create a simple PHP script that will list each item from the items table, providing a link to add each item to our shopping cart. |