Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2003, 06:24 PM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shabboleth
Default Cookies

Hello All,

I am trying to use cookies to get my application to recognize returning users. After logging in I want to set a cookie using the following line of code:

setcookie("username", $username, time() + 31536000);

But it doesn't seem to be working. I have tried to access the cookie using $_COOKIE['username'],and $HTTP_COOKIE_VARS['username'], and I always get a null value.

I have also tried:

if(isset($username))

and it always comes up negative.

Can anyone tell me what I am doing wrong?

Glen
 
Old December 29th, 2003, 06:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Okay, be a little more explicit -- Are you setting the cookie on the same page that you're checking it's existence on?

A cookie variable won't exist in $_COOKIE until you load another page under the same path for which the cookie is valid.


Take care,

Nik
http://www.bigaction.org/
 
Old December 29th, 2003, 06:57 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Also, you should check the return value of setcookie(). It should return TRUE if it ran successfully. Note that this doesn't indicate whether a user accepts the cookie -- TRUE here just means that setcookie was able to properly generate and send the proper HTTP header.


Take care,

Nik
http://www.bigaction.org/
 
Old December 30th, 2003, 10:32 AM
Registered User
 
Join Date: Aug 2003
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to shabboleth
Default

OK, I checked the return value of setcookie() and it was setting fine. My problem was something else.

I appreciate everyone's help getting past these basic mistakes.

Glen






Similar Threads
Thread Thread Starter Forum Replies Last Post
Cookies hastikeyvan Classic ASP Professional 5 May 29th, 2006 07:36 AM
Should I use cookies?? Help Tee88 Classic ASP Basics 3 January 21st, 2005 12:33 AM
Cookies cjo ASP.NET 1.0 and 1.1 Basics 1 November 8th, 2003 02:38 PM
Help!About Cookies jelsen ASP.NET 1.0 and 1.1 Basics 2 September 9th, 2003 08:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.