Page 1 of 1

Generating Cookies Through Form

Posted: Mon Apr 02, 2007 6:41 pm
by PHP-Learner
So, I'm trying to slog my way through the world of PHP, and for whatever reason, I can't get a form to set/generate a cookie. The solution is currently beyond me and so I turn to the masters.

The case at hand, is I need a form that a user can either accept or not, terms set by the company. If they do acept the terms, I need to set a cookie so that don't have to agree to terms rarely, if at all. However, my issue begins with getting the form to create the cookie, which means I can't do anything else.

The code:

Code: Select all

<?php
// if form was submitted, handle it
if(isset ($_POST['submit'])) {

	//sending to cookie monster
	setcookie ('termsAgree', $_POST['termsAgree'], time()+30000000000000, '/', '', 0);
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1" />
		<meta name="generator" content="Adobe GoLive" />
		<title>Poo bear Agreement</title>
	</head>

	<body>
		<?php 
		// if cookies were sent, print message
		if (isset ($_COOKIE['termsAgree'])) {
		print 'You have accepted the terms you may <a href="#">proceed</a>.';
		} else {
		print '<p>In order to go any further along this process, you must agree to SHOOP Terms and Conditions.</p>';
		}
		?>
		
		<form action="terms.php" method="post">
			<input type="radio" name="termsAgree" value="termsAgree" /> Agree
			<input type="radio" name="termsDagree" value="termsDagree" /> Disagree
			<input type="submit" name="submit" value="submit" />
		</form>
		
</body>
</html>

I'm sorry if something liek this has been posted before, I did a search and didn't find anything that addressed my needs. Any help on this would be greatly appreciated.

Posted: Mon Apr 02, 2007 6:52 pm
by RobertGonzalez
Maybe try...

Code: Select all

<?php
$expire = 60 * 60 * 24 * 365; // One year, in seconds
if (isset($_POST['termsAgree'])) {
    setcookie ('termsAgree', 1, $expire);
}
?>
On a side note, this is not something that should be handled by cookies but rather a database entry in the users table. If the user doesn't accept cookies they will essentially be not allowed in the door because the cookie will never be found.

Posted: Mon Apr 02, 2007 7:15 pm
by PHP-Learner
I like going the route of variables to set up time, here, which certainly helps with the length in time aspect, however, the cookie still isn't generating.

I've looked at various cookie tutorials and it always seems I'm more or less on the right path, but for some reason I can't generate a cookie at all. I've tested this on several platforms, browsers, all with cookies allowed and...nothing.

is there something in my setcookie (and I guess, in your example) that is off? I've read in books about a wonkiness of cookies working or not, could this be a case of that?

As to your concern about cookies being turned off, and locking people out, it's very valid, and something I addressed (in part) with the client. Unfortunately, they are entirely too cautious of databases, and they do NOT want to go the route of users and such. Thus, I'm in this situation.

Thanks for your help.

Posted: Mon Apr 02, 2007 9:31 pm
by feyd
I hope you realize that 30000000000000 is well outside the limit PHP will parse as an integer.

Code: Select all

[feyd@home]>php -r "echo (int)30000000000000;"
-346562560
on my local machine.

Posted: Tue Apr 03, 2007 10:26 am
by RobertGonzalez
Do you have a full example code setup that we can see to make sure there is not code glitches anywhere? Cookies generally work as long as you call them with the right params (which is why I suggested the time setup the way I did, as the 3000000000000000000000000000000000000000 number you are using will make PHP want to throw up.

Also, how are you verifying that the cookies are not set? Checking your browser cookies? Script side perhaps? More information please.