Jump to content

Cookie Question


JSHINER

Recommended Posts

Does anyone have a sample code I can look at that sets a cookie upon checking a box.

The form will have a text box asking for zip, and an option to remember zip. If it is checked, next time a visitor comes to the site they will go to:

www.site.com/index.php?action=search&zip=$zip.

I also need the form to send them to that specified search page, which it already does, but I also need it to set the cookie.

Any help / pointers would be greatly appreciated. I have never worked with cookies before.

Thank you!
Link to comment
Share on other sites

I haven't tested it, but it should work: [code]<?php
ob_start();
if(!empty($_POST['zip']))
{
echo "Your zip is {$_POST['zip']}";
if($_POST['remember_zip'])
{
echo "<br />We are remembering your zip code.";
setcookie("zip", $_POST['zip'], time()+3600*24*365); // store it for one year
}
}
else {
echo <<<EOF
<form action='{$_SERVER['REQUEST_URI']}' method='post'>
<label>Zip: <input type='text' name='zip' /></label><br />
<label><input type='checkbox' name='remember_zip' value='1' /> Remember zip code</label><br /><br />

<button type='submit'>Submit</button>
</form>

EOF;
}
ob_end_flush();
?>
[/code]

Note: The reason I can send headers (set a cookie in this example) is that I use output control.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.