Professional CodeIgniter

(singke) #1

Building a Shopping Cart


In Chapter 4 , you learned how to use CodeIgniter ’ s MVC framework to create a basic eCommerce
site, with its home page, category views, and product detail pages. In this chapter, you ’ ll learn how
to create the Shopping Cart. You ’ ll also get feedback from the client on how to polish the site and
then implement those changes inside your CSS and PHP files.

Displaying the Shopping Cart


At some point in any eCommerce site, site visitors add products to their shopping carts and then
try to check out. By the end of the chapter, you ’ ll have a working shopping cart (but no checkout
function, as you ’ ll need to confer with the client about this).

The approach you ’ re going to take with Claudia ’ s Kids is a simple, straightforward approach that
makes use of both CodeIgniter ’ s session object and native PHP sessions. If you ’ re used to working
with PHP sessions, you might be tempted to think that CodeIgniter ’ s session object is the same
thing. CodeIgniter sessions are different from PHP sessions, acting just like cookies. They can be
encrypted and can store serialized data that are automatically created or updated as needed.

To use CodeIgniter sessions, all you need to do is load the library. In this case, you ’ ve already
loaded the Session library in the autoloader.php file (in Chapter 2 ), so all you have to do now is
use the appropriate functions.

Because CodeIgniter sessions are cookies, you have the same limitations as you do with cookies
in general. For example, cookies can be tampered with by the end - user. This makes CodeIgniter
sessions (even if the data in them are encrypted) hard to trust. Another limitation is the fact that
a session cookie only holds 4 KB (kilobytes) of data. It is possible for someone to fill up their
session cookie either accidentally or with malicious intent.
Free download pdf