Sign In | Subscribe
Start learning today, and be successful in your academic & professional career. Start Today!
Loading video...
This is a quick preview of the lesson. For full access, please Log In or Sign up.
For more information, please see full course syllabus of Advanced PHP
  • Discussion

  • Download Lecture Slides

  • Table of Contents

  • Transcription

  • Related Services

Bookmark and Share

Start Learning Now

Our free lessons will get you started (Adobe Flash® required).
Get immediate access to our entire library.

Sign up for Educator.com

Membership Overview

  • Unlimited access to our entire library of courses.
  • Search and jump to exactly what you want to learn.
  • *Ask questions and get answers from the community and our teachers!
  • Practice questions with step-by-step solutions.
  • Download lesson files for programming and software training practice.
  • Track your course viewing progress.
  • Download lecture slides for taking notes.
  • Learn at your own pace... anytime, anywhere!

Web Application Development

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

  • Intro 0:00
  • Version 12.0 Overview 0:12
    • Version 12.0 Changes Part I: Updating 'viewCart.php' & 'checkout.php'
    • Version 12.0 Changes Part II

Transcription: Web Application Development

Hello again, and welcome back to Educator.com's Advanced PHP with MySQL course.0000

In today's lesson, we are going to be continuing development of our web application.0005

And we are going to be incorporating what we learned in our last lesson regarding sessions.0008

In this lesson, we are going to go over one new version of our web application; it is going to be called version 12.0.0014

And what we are doing is converting the web application from using client-side sessions to manage our shopping cart.0019

And the way that worked is: we were sending cookies to the client that contained all of the information about a shopping cart.0028

For example, for each item we added to a cart, we would create a cookie with that item number,0035

and then set the cookie's value to the quantity of that item in the cart, 0039

and send that cookie to the user that would be stored on the client side.0043

And then, every time the user would go back to our website to view the cart or to add another item to the cart,0046

it would send all of these cookies back; and that is how we maintained the state of our shopping cart.0053

We maintained it on the client side, in what was essentially a client-side session.0057

Now, we are going to be converting over to using PHP's built-in server-side session functionality.0062

Again, we are just converting using client-side cookies to storing all of our shopping cart state on the server side.0070

What we have done before: if we look at our previous version of viewCart.php,0078

in the beginning of the script, what we would do is load our shopping cart object, 0084

or build our shopping cart object up, using this buildCartFromCookies method.0091

And we would pass it the _COOKIE superglobal, which would contain any cookies that we had previously set for items we would add to our cart.0095

This function, as we learned before, would go through, look at all the information in these cookies, and build our up our shopping cart object.0102

Then, what we could do is: if we needed to add an item to the cart, what we would do would be 0109

to make an update to the cookie for that particular item.0114

We would increase its quantity by 1; or if it was a new item, then we would just set a brand-new cookie for it with the value equal to 1.0117

In addition, if we wanted to remove an item from a shopping cart, 0126

we would have to set the cookie for that particular item, using the item ID, and set it to expire in the past.0129

And that would essentially delete that cookie, so that the next time the user views your page, it doesn't get sent back to our web server.0138

And therefore, the server doesn't know that that item is in the cart anymore.0144

That is how we removed an item from a cart.0148

Well, now what we have done in our new viewCart.php is: we are making use of sessions to store this information, and it really simplifies things a lot.0154

In our viewCart.php, the first thing that we do in any page where we use sessions is:0164

we call our session_start method, which is going to generate a new session and a new session ID, 0167

if one has not already been created--if no session ID cookie was passed to the server.0174

If not--if it is a continued session--what it is going to do is look at the cookie passed by the user,0181

look up the data associated with that session ID, and load it into the session variable.0189

What we can see here is that the way we load our cart method now--rather than using the buildCartFromCookies,0195

we simply test if the session variable cart exists, using the isset construct,0201

which means that we have previously had a Cart object in our session.0207

And I should also mention: the way we implement our shopping cart using PHP sessions is:0212

we store a shopping cart object in the session array; it is a Cart object, and it has the name cart.0219

This is testing to see, "Does this cart session variable exist?" (which is a Cart object).0228

"If so, load it from session into this cart variable; if not, just create a new, empty Cart object."0233

And then, what we do is: whereas before we were looking at cookies...if we wanted to add an item,0242

we would have to set a new cookie that would set it with a quantity incremented by 1.0249

If we wanted to remove an item, we would have to call the setCookie method and set the cookie for a particular item to the past.0254

Now, what we are doing is simply using the cart's built-in methods to add items to and remove items from the cart.0260

Here, we have the addItem object method and the removeItem object method.0267

And we use those to update our cart, and then what we can do is: to save the cart between sessions,0271

rather than sending cookies back to the user with all of this information,0277

we just save this Cart object that we have updated to our session, using the _SESSION superglobal.0281

So now, this updated cart will be added to the session.0290

The next time that we view a page on our website that calls session_start, we are going to have access to that cart.0293

And in particular, it is going to be checkout.php.0299

What we have done, as is mentioned, is: in viewCart.php, we used the object methods of our Cart object0306

to update a shopping cart, as opposed to updating the cart with setCookie calls.0312

And then, we save our shopping cart object to a session variable named cart.0317

Now, what that is going to do is: we also have updated checkout.php so that, when it calls session_start0323

at the beginning of that page, it is going to have access to this shopping cart that was saved in viewCart.0330

So, let's say we have added a couple of items to our shopping cart.0335

When we go to checkout.php, it is going to be able to load that shopping cart object from the session, and then successfully process the order.0338

If we take a look at, for example, our old checkout.php, what it did was the same thing that our viewCart.php did.0346

It would look at all the cookies sent over from a client that contains information about a shopping cart,0354

and build up that shopping cart, and then it would go ahead and process the order.0359

And one of the things that it would do was: after the order was completed, it would go through 0363

and specifically delete each cookie for each item that was in the shopping cart by manually calling this setCookie method--0367

which was somewhat tedious; and so, we are going to see that that is much simplified in our newer version.0375

Now, when we complete an order, and we want to empty the shopping cart,0381

instead of having to unset all of these cookies, what we are going to do is just unset a session variable.0385

If we look at our new checkout.php, we can see that we call session_start at the beginning,0390

which is going to give us access to this _SESSION superglobal.0394

We are going to test to see if the cart session variable exists (which would be a Cart object).0398

If it does, we load it from the _SESSION superglobal into cart; if not, we create a new Cart object.0406

Then, we simply perform a test that says, "As long as the cart is not empty, then we are going to process it."0411

"If not, we are going to output an error that says your cart is empty," because what that means is:0415

if there was no cart that was previously set up for the session, it means that there were no items in the shopping cart.0424

And so, that is how we test for that.0429

Once we have set that cart variable to the shopping cart object from our session, 0433

we go ahead and follow through with completing the order, as we had done before.0439

The one difference is that now, when we empty the shopping cart after an order is successfully completed,0444

as opposed to looping over the _COOKIE superglobal and deleting all the cookies for our items in the shopping cart,0450

we are simply going to unset the session variable cart.0455

What that is going to do is delete it from the session.0459

So now, when we go back and view our cart page, specifically what would happen is:0463

at the beginning of viewCart, it would try to load the cart from the session; it wouldn't exist, and so it would have an empty cart.0468

And then, when we have an empty cart, it would show a particular message.0478

Let's go ahead and take a look at how this works in action.0481

If we go to Electronics, for example, and let's add a television to our cart:0486

well, when we go to viewCart.php, what it is going to do is start a session.0494

We can see that it sets a session cookie, PHPSESSID, and that is what we are going to pass 0497

back and forth between the client and the server in order to maintain state.0501

And then, let's add another item, a Sporting Goods product--let's add a basketball to our shopping cart.0506

We go to Add to Cart; now, what we can see is that our shopping cart contains both items.0513

The way it is able to do that is that, when that new viewCart page is loaded, it starts a session, 0518

and it is able to go and look up this cart session variable, load the cart information,0527

and then viewCart knows how to output that cart to display the items in the cart.0533

So now, let's say we want to go ahead and check out.0540

We'll just enter some sample information.0544

And so, now what has happened is: this checkout.php knows about our cart, and that is how it is able to calculate the total,0556

because at the beginning of checkout.php, it loads the cart from the session.0565

So, that is how it is able to calculate the cart total here, which we are outputting for the user.0570

And when we submit the form, we can see that the order was successfully completed.0575

It has access to the cart total.0586

And one thing to notice is that we can see, when we submitted this form, that our PHPSESSID cookie still exists.0589

We have ended the session, but our session cookie still exists.0601

That said, we have unset our shopping cart object.0605

So, if we go back to viewCart.php, it is going to show that our shopping cart is empty.0612

That shows that we have eliminated the shopping cart data from our session.0618

But again, if we look at this page, we can see that we are still passing this PHP session cookie from the user.0623

So, essentially, we have not effectively ended this session.0630

We have deleted our session data, but as far as our web server is concerned, every time we are requesting a page,0633

it is still considered part of the same session, because we are passing this cookie back and forth.0639

And that is something that we are going to address in our next lesson, 0643

which is going to talk about how to properly end and destroy a session, 0646

so that when you are done and your shopping cart is empty, that session is completely over.0650

That ends today's lesson; thank you for watching Educator.com, and I look forward to seeing you next time.0656