Page 1 of 1
[SOLVED] - Cookies not working
Posted: Mon Jul 02, 2007 7:11 pm
by iknownothing
Hey Guys, I have this piece of code...
Code: Select all
if (setcookie('planview', 'yes', time() + 86400 * 30) == TRUE) {
echo $_COOKIE['planview'];
}
else {
echo "Cookie no Workie";
}
What I have searched for over and over again, including PHP.NET, the above code should set a cookie, however, it just wont do it, my Browser allows Cookies, and I've combed all code above this with a fine tooth, and there is absolutely no output above it.
Can anyone tell me why this may not be working?
Thanks
Posted: Mon Jul 02, 2007 7:14 pm
by volka
_COOKIE contains the cookies the client sent with the current http request.
setcookie() adds a cookie to the response headers. If the client accepts the cookie it will send it back with the next http request.
Posted: Mon Jul 02, 2007 7:14 pm
by tecktalkcm0391
Because it never set a cookie.... you would have to do setcookie on one page and then check cookie on another, or some other way so the browser receives the cookie, then it can be called from in PHP
Posted: Mon Jul 02, 2007 7:29 pm
by iknownothing
Ok, thanks for the above resonses. The code I put in above, was my last ditch effort for posting on here, so I'll show you what I had before, the Cookie is not viewed until the page is changed in one way or another
Basically, what it is doing is, the user fills in a questionaire thing, submits it, then, by way of the cookie, is able to view certain pages of the site. FormThingo being the questionaire submission, $theplan is the page viewable with the cookie.
Code: Select all
if(isset($_POST['FormThingo'])){ // USER FILLS IN FORM
setcookie('planview', 'yes', time() + 86400 * 30) //SETS COOKIE
// EMAIL CLIENT THE QUESTIONAIRE DETAILS
include($theplan.".php"); //VIEW PLAN
}
elseif(isset($_COOKIE['planview'])) { // CHECKS FOR COOKIE ALREADY SET
include($theplan.".php"); //VIEW PLAN
}
else {
// QUESTIONAIRE
}
Thanks
Posted: Mon Jul 02, 2007 7:39 pm
by John Cartwright
Why not use sessions?
Posted: Mon Jul 02, 2007 7:46 pm
by iknownothing
So the user can go back to the site a week or 2 later and look again...
The website is a Home construction company, the questionaire is as long as time itself, and usually when people are looking to build a home, things in their life can get pretty hecktic, so I would rather that they only had to fill it in once while deciding on a home, just for a little less stress... Also, someone might be looking at it, then want to show his/her husband/wife later on, and filling in the form again would be a pain.
Posted: Mon Jul 02, 2007 8:33 pm
by alex.barylski
$_SESSION's are always prefered. They are more flexible and easier to work with.
Sessions are typically initialized and used with a "session" cookie (meaning the cookie is destroyed when the browser window closes) but this isn't by force. You can change the expiry time of SESSION's using the session API.
Code: Select all
session.cookie_lifetime integer
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means "until the browser is closed." Defaults to 0. See also session_get_cookie_params() and session_set_cookie_params().
Note: The expiration timestamp is set relative to the server time, which is not necessarily the same as the time in the client's browser.
This just changes the expiration of the cookie which holds the SESSIONID you will likely want to change the cache expiration as well otherwise you'll loose your session data.
http://ca3.php.net/manual/en/ref.sessio ... che-expire
If your storing FORM data you are really best off to use sessions.
Posted: Mon Jul 02, 2007 8:49 pm
by iknownothing
im not storing the form data, thats getting emailed to the site owner, then not getting viewed again. All I want to store is the word "yes", or maybe the number "1", just a small string or integer to tell the site that the user has filled the form in in the past, so they can access the pages which needed to form to be completed. No security is needed on this either, it is not of utmost importance that the user MUST fill in the questionaire, but my client would like it that way.
Posted: Mon Jul 02, 2007 10:55 pm
by iknownothing
Ok Guys,
I just did a test file with this code...
Code: Select all
if(!isset($_COOKIE['planview'])) {
if (setcookie('planview', 'yes', time() + 86400 * 30) == TRUE){
echo "Cookie Set";
}
else{
echo "Cookie Not Set";
}
}
elseif(isset($_COOKIE['planview'])) {
echo "Cookie Works";
}
The file works, and stores a cookie fine...
But when I use this part of the code...
Code: Select all
if (FORM SUBMITTED){
if (setcookie('planview', 'yes', time() + 86400 * 30) == TRUE){
echo "Cookie Set";
}
else{
echo "Cookie Not Set";
}
// FORM/EMAIL STUFF
}
In an IF statement asking if the form has been submitted, it comes back saying "Cookie Not Set". I dont get it, its the exact same code, its the VERY first thing inside the IF statement, what else could be stopping it??
Posted: Mon Jul 02, 2007 11:00 pm
by iknownothing
Sorry for wasting your time guys.
When I said I had combed my code for any output above the setcookie, I was kind of not telling the truth. See my page was being included into the template page, thus having output above it as a whole but not on the individual file.
Thanks for your help anyway.