I have added a custom field to the comments form of a wordpress installation. When submitting the form, I set a cookie with the member number in it (in the file wp-comments-post.php) added at line 13:
if (!isset($_COOKIE['member_number'])) { setcookie('member_number', $_POST['member_number']); }
This seems to work ok in terms of correctly setting a cookie. Then in pluggable.php, I have:
$member_number = $_COOKIE['member_number'];
which is just below the nocache_headers() function.
In theory, the cookie value for member_number is read, inserted into an email, and sent. Only 1 problem, the first time a user submits a comment, the member_number is not being printed into the email. If I submit another comment, the email gets the member_number from the cookie no worries....but the cookie of the previous post! For example, if I enter a comment with the member_number as 1234 the first time I visit the site, nothing is inserted into the email. If I then post a comment in another post with the member_number set to 0987, the value inserted into the email is 1234. Ie. the email value for member_number is always 1 post behind for a session.
What this tells me is that the code that sets the member_number in the cookie is not occuring in the right place - ie. it is happening too late. I am assuming it has something to do with headers. I know cookies need to be set before the page html output has occurred - ie. they should occur in the headers, but how can I set the same code in the header? ie. determine if the cookie exists, if not, set it based on the post value from the form.
I hope this makes some sense.
Any help would be greatly appreciated.
Problem creating a cookie
Moderator: General Moderators
-
phpnewbieperson
- Forum Newbie
- Posts: 22
- Joined: Wed Mar 26, 2008 8:25 am
-
phpnewbieperson
- Forum Newbie
- Posts: 22
- Joined: Wed Mar 26, 2008 8:25 am
Re: Problem creating a cookie
Any ideas?