Lecture 5 - Cookies, sessions and file writing - annotated

Lecture 5 - Cookies, sessions and file writing - annotated...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Cookies, Sessions, and Files INFO/COM S 230: Intermediate Web Design and Programming Cookies Cookies Cookies are one way to save limited amounts of information between visits of a user. Saving a cookie setcookie($name, $value) setcookie($name, $value, $expiration) Without $expiration argument, cookie goes away when the browser closes. $expiration is number of seconds after January 1, 1970. Set as follows: setcookie(“name”, “value”, time() + 60*60); Must be at the top of the .php file ( before the DOCTYPE or any other HTML). Getting a cookie If a cookie was set using setcookie(“name”, “value”), and the expiration date has not passed, then when the user returns, the variable $_COOKIE[“name”] will contain “value”. <?php setcookie("username",$_POST["username"], time()+60*60); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <?php if (isset($_COOKIE["username"])) { print("<p>Welcome, ".$_COOKIE['username']."! </p>"); } elseif (isset($_POST["username"])) { print("<p>Welcome, ".$_POST['username']."! </p>"); } else { ?> <form method="post" action=“cookie-form.php"> <p>What is your name? <input type="text" name="username" /> </p> <input type="submit" value="Click to submit" /> </form> <?php } ?> Easy PDF Creator is professional software to create PDF. If you wish to remove this line, buy it now.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 How they work Sessions Using cookies is one way to save user data between times they access a
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 6

Lecture 5 - Cookies, sessions and file writing - annotated...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online