Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (!isset($_POST['email'])) {
- // if form has not been submitted
- // display form
- // if cookie already exists, pre-fill form field with cookie value
- ?>
- <html>
- <head></head>
- <body>
- <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
- Enter your email address: <input type="text" name="email" value="<?php echo $_COOKIE['email'] ?>" size="20">
- <input type="submit" name="submit">
- <?php
- // also calculate the time since the last submission
- if ($_COOKIE['lastsave']) {
- $days = round((time() - $_COOKIE['lastsave']) / 86400)
- echo " $days day(s) since last submission"
- }
- ?>
- </form>
- </body>
- </html>
- <?php
- }
- else {
- // if form has been submitted
- // set cookies with form value and timestamp
- // both cookies expire after 30 days
- if (!empty($_POST['email'])) {
- setcookie("email", $_POST['email'], mktime()+(86400*30), "/")
- setcookie("lastsave", time(), mktime()+(86400*30), "/")
- echo "Your email address has been recorded."
- }
- else {
- echo "ERROR: Please enter your email address!"
- }
- }
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment