Understanding Cookies and Managing Them in Web Development
Explore the intricacies of cookies in web development, including how they are stored, set, deleted, and read. Learn about the importance of timing in handling cookies, setting cookies in PHP, and managing sessions along with cookies on different platforms. Dive into practical examples and guidance on utilizing cookies effectively.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Cookies 12/1/2022
Wheres the Data? Cookies are stored on the client computer If no date is set, in the RAM being used by the browser and vanish when the browser is closed If date in the future is set, on the user s system drive If date in the past is set, the cookie is to be deleted
Timing is Everything Cookies are copied into PHP s $_COOKIE array when your page is loaded setcookie() saves things on the client computer, not in the $_COOKIE array So if you want new cookie data to be used in your program, you ll need to do BOTH setcookie() the data Put it in a PHP variable
How to set a cookie Send it before any HTML is sent Once HTML has been sent, you can t send cookies setcookie(name,value,expire,path,domain,secure,httpo nly) All except the name and value are optional Example from the w3schools $cookie_name = "user"; $cookie_value = "John Doe"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day ?>
How to Delete a Cookie Use setcookie(), giving it a date in the past so it will be deleted as expired
How to read a cookie if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; }
Algorithm Capture cookies into PHP variables Capture GET or POST variables indicating work to be done setcookie() as needed HTML Possibly including form with things to be done Possibly including PHP showing defaults/progress
Sessions & Cookies Where? On Linux server: /var/lib/php/sessions On Chrome: on the right More tools Developer Tools Application Storage Cookies