Cookies and a Self-Submitting Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Cookies and a Self-Submitting Form

Post by makamo66 »

I am using setcookie in a self-submitting form. Once the form is submitted, it sends an email and then it redirects to the next page by using a header. The problem is that the cookies aren't set for the email even though they are set (and displayed) on the next page. I've tried using the following code to reload the page before I send the email but to no avail:

setcookie("survey_username", $_POST['yourname'], time() + 31536000, '/survey/', $_SERVER['SERVER_NAME'], 0);
header("Location: ". $_SERVER['PHP_SELF']);
exit;
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Cookies and a Self-Submitting Form

Post by Jonah Bron »

Try redirecting with a <meta> refresh. The redirect header might be colliding with the cookie. :roll:
makamo66
Forum Newbie
Posts: 24
Joined: Wed May 12, 2010 6:13 pm

Re: Cookies and a Self-Submitting Form

Post by makamo66 »

I ending up redirecting with a header to a new page and then only after that sent the mail and then the cookies were set properly. I'm not that happy with the solution though as it meant I had to add three new pages and that seems kind of messy. I should have been able to reload the page after setting the cookies to make it work.
Post Reply