Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cookies BIS1523 – Lecture 23.

Similar presentations


Presentation on theme: "Cookies BIS1523 – Lecture 23."— Presentation transcript:

1 Cookies BIS1523 – Lecture 23

2 Cookies? Third party cookies? Cookies containing arrays
<?php // set the cookies setcookie(“blah[three]", "cookiethree"); setcookie(“blah[two]", "cookietwo"); setcookie(“blah[one]", "cookieone"); Using to ‘remember’ something (login, or the last thing clicked, or whatever) Or used for shopping cart – list of products, an ‘add to cart’ button, store in a cookie, refresh with list of whats in the shopping cart, etc. Demonstrate as an alternative to using files. Third party cookies?

3 Cookies Cookies are a way for a web server to store information on the user’s computer. By doing so, the server can remember the user over the course of a visit, or through several visits. One way to think of cookies would be similar to a name tag. You tell the server your name, and it gives you a name tag. When you visit the site, the server can check the value of the name tag to remember your name Because cookies are remembered (and associated with) each clients computer, you don’t have to keep track of which client is accessing the website. Using the previous example, imagine if instead of giving the user the name tag, you put it in a drawer in your desk. Now when a user comes into your office, you have to figure out which name tag is theirs.

4 Creation Process Cookies are created with the “set_cookie” command.
It is important to understand how and when and where they are created when using that command. Typically, a user will fill out a form, click the ‘submit’ button, and information from that form is sent to the server (in a POST or GET method). The receiving web page will then take some of that information, or generate some of its own, and use the set_cookie command to remember information. Timing is important, the ‘results’ page wont have access to the cookie, but the _next_ page read will.

5 Example We have an “index.php” that contains a form
This form calls “results.php”, and sends in the value “temp-in” as input from that textbox.

6 Example Results.php could, set a cookie to remember “the last temperature you looked for”, or even “the last result”, or both. Now, results.php would not be able to have the cookie, as it is created when results.php is sent back to the users computer. But any future PHP program (from the same web server) could retrieve the results of that cookie.

7 Cookie Creation Timeline
If we included a link back from results to index.php, index.php could read, and use that cookie.

8 setcookie To use set_cookie, you provide the name of the variable, and the value you want it to be setcookie($name, $value); Cookies are sent out with the header information, so you have to put all set_cookie commands at the very top of the PHP program, before the header has been sent.

9 Reading Cookies Values from cookies will be sent into a program using the $_COOKIE array. It works just like the post array, so to read a cookie, you could do something like:

10 More Timing A cookie is never accessible to a script immediately after it’s been sent. You can’t do this: That cookie is sent back to the web browser, but doesn’t ‘exist’ until it has been sent, which is after this script is done

11 No Cookie Users can disable cookies within their browsers.
However, with just PHP, it is difficult to check to see if cookies are enabled, because we cant set one (to test), and then read it in the same session. To do this would require reloading. You should always code your pages in such a way to report valid error messages or notes if cookies are required, but not present.

12 Debugging Cookies The easiest way to debug working with cookies is to use the print_r command to print the entire $_COOKIE array. Then, as you click through your pages, you can see what the cookies are doing. To remove a cookie, just use setcookie to set its value to false setcookie(PreviousTemp, FALSE);

13 Arrays Cookie variables can be arrays.
This code would create a blah[] array with three values You could retrieve it by doing

14 Shopping Cart Example One common example of using cookies is to implement shopping carts that are ‘remembered’ over several web site visits. This example program uses a file to store product information in, prints it on the screen, and allows the user to ‘add stuff to the cart’.

15 The Product List The product file contains 2 fields, a name and a price. We use a table to print out that info onto the left pane of our form.

16 Distinguishing Items We want to print out a number of different buttons, and be able to tell which one the user picked. We’ll do this by giving the textboxes next to the products an array We use $i as a counter inside our loop, and build the names using that.

17 Results So, if the user types in a number in row 4 and clicks “add to cart”, then P[4] will have a value of “4” To query it, we would do:

18 The Cookie We have a few options on how to store information in the cookie. We could use an array like we did with the textboxes, so P[0] would be the number of item 0 in our cart. We could also just use variables named P0, P1, P2…. The variable method would look like: $i would be the row number.

19 Adding To the Cookie We have a list of everything in the cart in $_COOKIE, if they clicked “add 2 things” to the cart, and there were already 2 things, we need to now have 4 things So we take the variable we read in, and the cookie, add them together, then store them back in the cookie.

20 Example Let’s assume the cart is empty, and the user clicked 2 on row 1. When ‘add to cart’ is pushed, $_POST[P[2]] will have a value of 2. When read in the P array into $CartListAdd, then search it:

21 Example So row 1 has a value of 2. $_COOKIE has no values in it.
$i will be a 1 when our search condition is met The variable $value will then be set to (results in 2) So the setcookie command will set P1 equal to 2. So the next time we come to the site, P1 will exist.

22 Reading the Cookie To print out our shopping cart, we loop through every cookie, and if it is greater than 0, print it out. However, we cant just test the cookie, as any ‘new additions’ aren’t accessible in this .php program. So we once again need to add the two together.

23 Finished program The finished program has 3 parts:
At the very beginning, check to see if we ‘added’ anything to cart, if so, loop through every thing, and total the new stuff with the stuff already stored in $COOKIE In the middle, print out all the products on the left At the end, print out all the stuff in the cookie & new stuff combined.


Download ppt "Cookies BIS1523 – Lecture 23."

Similar presentations


Ads by Google