dan2684 Posted December 24, 2008 Share Posted December 24, 2008 Hello all, I've added this to my product page: setcookie("lastViewed",$_GET['prodId']); ...but it's not setting the cookie! Any ideas? Thanks, Dan Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/ Share on other sites More sharing options...
revraz Posted December 24, 2008 Share Posted December 24, 2008 If you want to cookie to remain after you close the browser, you need to set a time duration. Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723231 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 Sorry, I've tried it with a time duration, and I've never closed my browser. Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723234 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 example: cookie will expire in 1 hour: setcookie("lastViewed", $_GET['prodId'], time()+3600); Make sure you set the cookie before the tag. Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723235 Share on other sites More sharing options...
LemonInflux Posted December 24, 2008 Share Posted December 24, 2008 mm, cookies. so have you tried turning error reports on? Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723238 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 no, haven't tried error reports - how do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723242 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 Put this at the top of your script: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723243 Share on other sites More sharing options...
revraz Posted December 24, 2008 Share Posted December 24, 2008 How do you determine the cookie is not being set? Have you confirmed your $_GET variable? Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723244 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 Maq- no errors revraz - because I'm trying to echo it out on another page Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723246 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 You do something like: if (isset($_COOKIE["lastViewed"])) echo "Last product_id viewed: " . $_COOKIE["lastViewed"] . "! "; else echo "You have never viewed a product! "; ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723247 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 Yup - I know. The cookie isn't there. Does that fact that it's running locally make a difference? I know that sometimes things like mail() don't work until I make it live. ie. are there settings in Apache that I can change? Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723248 Share on other sites More sharing options...
revraz Posted December 24, 2008 Share Posted December 24, 2008 If you want help, provide both the code that sets it and the code that reads it. We can only guess at the problem if you don't provide enough info. Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723251 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 Apologies for not being clear. Product page: setcookie("lastViewed",$_GET['prodId'],time()+3600); Homepage: echo $_COOKIE["lastViewed"]; Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723253 Share on other sites More sharing options...
Maq Posted December 24, 2008 Share Posted December 24, 2008 There must be an external factor. Does $_GET have a value? I just tried: setcookie("lastViewedTEST", 45,time()+3600); echo $_COOKIE["lastViewedTEST"]; ?> And it echoed out 45... Maybe you should post all the relevant code. Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723256 Share on other sites More sharing options...
revraz Posted December 24, 2008 Share Posted December 24, 2008 If you are using sub domains, make sure you use the domain switch when you set the cookie. Also, look at your cookies in the cookie folder, do you see it being created there? Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723259 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 I've just checked my cookies folder - it IS there and it has the correct value. It's just not echoing out... what's also weird is that it echoes out on the page where I set the cookie, but not the homepage. Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723261 Share on other sites More sharing options...
revraz Posted December 24, 2008 Share Posted December 24, 2008 setcookie("lastViewed",$_GET['prodId'],time()+3600, "/"); Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723273 Share on other sites More sharing options...
dan2684 Posted December 24, 2008 Author Share Posted December 24, 2008 I really didn't know you had to put it in the same directory! Thank you revraz - you are a genius :-) Quote Link to comment https://forums.phpfreaks.com/topic/138320-solved-cookies/#findComment-723280 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.