Jump to content


Photo

Cookie Question


  • Please log in to reply
7 replies to this topic

#1 JSHINER

JSHINER
  • Members
  • PipPipPip
  • Advanced Member
  • 465 posts
  • LocationBoston, MA

Posted 21 September 2006 - 06:01 PM

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!

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 06:12 PM

I haven't tested it, but it should work:
<?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();
?>

Note: The reason I can send headers (set a cookie in this example) is that I use output control.

#3 JSHINER

JSHINER
  • Members
  • PipPipPip
  • Advanced Member
  • 465 posts
  • LocationBoston, MA

Posted 21 September 2006 - 06:18 PM

I need that form to perform an action too... Can that be done?

#4 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 06:32 PM

Sure... what kind of action should be performed?

#5 JSHINER

JSHINER
  • Members
  • PipPipPip
  • Advanced Member
  • 465 posts
  • LocationBoston, MA

Posted 21 September 2006 - 06:46 PM

The current form:

<form action="search2.php?action=search" method="post">

Also, in action=search the zip code is validated, so could the cookie only be set if the else is true?

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 06:55 PM

I'm not sure what you mean, but just change
if($_POST['remember_zip'])
to
if($_POST['remember_zip'] && *do validation stuff here*)

(You obviously need to do some validation where it says *do validation stuff here*)

#7 JSHINER

JSHINER
  • Members
  • PipPipPip
  • Advanced Member
  • 465 posts
  • LocationBoston, MA

Posted 21 September 2006 - 07:06 PM

I need the form to post to the action=search. The validation is there.

#8 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 21 September 2006 - 07:07 PM

Just change the url in the action attribute on this line:
<form action='{$_SERVER['REQUEST_URI']}' method='post'>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users