<style> .reveal { font-size: 27px; } .reveal div.para { text-align: left; } .reveal ul { display: block; } .reveal ol { display: block; } img[alt=drawing] { width: 200px; } </style> # COMP1010 ## 5.4 Web Apps: Cookies --- ## What Are Cookies? * Data * Stored on the user's computer * Sent back and forth inside the HTTP header --- ## What Are Cookies Used For? <ul><li>Session management</li> <ul> <li>logins</li> <li>shopping carts</li> <li>game scores</li> <li>anything else the server should remember</li> </ul> </ul> <span><!-- .element: class="fragment" data-fragment-index="1" --> <ul> <li>Personalization</li> <ul> <li>user preferences</li> <li>themes</li> <li>other settings</li> </ul> </ul> </span> <span> <!-- .element: class="fragment" data-fragment-index="2" --> <ul> <li>Tracking</li> <ul> <li>recording and analyzing user behavior</li> </ul> </ul> </span> --- ## Flask - Session * Allows us to save session data * Handles all the passing the session data to the browser * Allows us to retrieve session data * Handles all the receiving the session data from the browser --- ## Detour: Encryption * Example of simple encryption --- ## Secret Keys ### What is a Secret Key? * Used to encrypt the content of the cookie so that they can't be read (without the encryption key and the algorithm). * If they are changed, the server will reject the cookie and not use it. --- ## Secret Keys ### Creating a Secret Key * Run this code once, outside of your program: ```{python} import secrets secrets.token_hex(32) ``` * It will produce a string which looks something like this: `'8f42a73054b1749f8f58848be5e6502c'` --- ## Secret Keys ### Putting a Secret Key into your Application * Copy and paste that string into your program as the value of `app.config['SECRET_KEY']` --- ## Flask - Session ```{python} from flask import Flask, request, session app = Flask(__name__) app.config['SECRET_KEY'] = '8f42a73054b1749f8f58848be5e6502c' ``` * works as a dictionary for storing, checking if it has, and retrieving * some useful extras: * `session.clear()` * `session.modified = True` (when we change an int, float, string or boolean, it automatically notes that the session has been modified and needs to be stored again, on the other hand, when we append to an existing list, the session doesn't notice, and we need to manually set the modified state to True) --- ## Also Useful ```{python} session.get('variable', default_value) ``` --- ## Demo 1 * Create a simple application which saves a range of things in cookies. * As the user, how do we get rid of the cookie? --- ## Demo 2 * Write the Guess My Number application so that it stores the `guess`, `range_high` and `range_low` in a session object (and saved as cookies). --- ## Demo 3 * Shopping list (storing lists and dictionaries in the session). ---
{"metaMigratedAt":"2023-06-16T21:21:58.473Z","metaMigratedFrom":"YAML","title":"5.4 - Cookies","breaks":true,"slideOptions":"{\"transition\":\"slide\"}","description":"Data","contributors":"[{\"id\":\"969c3c3d-0ef4-4f08-b22a-2f2b8951224b\",\"add\":2992,\"del\":41}]"}
    610 views