[SOLVED] - Cookies not working

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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

[SOLVED] - Cookies not working

Post 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
Last edited by iknownothing on Mon Jul 02, 2007 11:01 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
Last edited by volka on Mon Jul 02, 2007 7:15 pm, edited 1 time in total.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post 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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Why not use sessions?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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??
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
Post Reply