Handling shopping carts and logins

One of the difficulties of the web is it’s statelessness. What does that mean exactly? The web was designed to show text content with some pictures. The web part of it was using hyperlinks to allow people to easily navigate between these web pages. At the time, everything was static, non-changing content. Basically web pages we treated like magazine articles.

That has all changed. Now, almost every web site is dynamic with programming and database content. The problem comes in trying to do things like shopping carts or logins. When your web browser requests a web page, it connects to a web server and asks for the page. The web server sends the page and closes the connection. When you then click a link to go to a new page, it again connects to the web server and gets the page. There is no mechanism for the web server to know that this is the same person that asked for page1 a couple minutes ago. So when you have a shopping cart, you are in trouble. There is no way for the products page to know about the items in your shopping cart. The pages can’t communicate between themselves to store information.

To work around this problem, we need to use programming. In this case, we would use SESSION variables. A variable is a container (think of it as a box) that stores information. Think of it as a box. A variable called Last Name would have ‘inside’ your last name. You can change what is ‘inside’ the variable, hence the name variable. SESSION variables are specific variables with a special function. These variables are set to contain information, but they also are associated with the particular person and browser that was on the page when it was set.

So, when you go to a product page and choose a product to add to your cart, the server does several things. First, it talks with the browser to see if a SESSION variable with the shopping cart id is already present. If not, the server makes a new SESSION variable with the shopping cart id and associates it with your browser. Then, whenever you go to the view cart page, the server sees which browser is there and finds the associated SESSION variable that has the shopping cart id. It then knows what id to look in the database for the find all the products in that shopping cart.

SESSION variables also work similarly when you login to a password protected area. An id gets set as a SESSION variable so the computer knows that you logged in successfully and it can use the id that is stored to pull only your information from the database.

SESSION variables are generally invisible to the user and the code doesn’t show up on the web page. It is a fairly effective way to keep some sort of information about you between pages. Once you close your browser, the SESSION ends. The next time you are on those pages, a new SESSION variable with a new id is created.

Comments are closed.