NYCPHP Meetup

NYPHP.org

[nycphp-talk] why does setcookie fail?????

Hans Zaunere hans at nyphp.org
Sat Sep 6 17:21:40 EDT 2003



Phil,

Phil Powell wrote:
> Well this is how I solved it:
> 
> if ($hasLoggedIn && ($row = mysql_fetch_row($query)) {
>  setcookie('nordicnet_registration', $row[0], 0, '/');
>  ...
> }
> 
> That works!! Apparently the problem was neither setcookie nor header, AND it
> was BOTH!

I just jumped into this thread, but it seems the development process is a bit, uhh, erratic :)

Consider that you have have three essential peices of logic here, all of which have to come together to give the end result you require (logging, and remaining logged in).  They are:

1) Database query and fetching of the result to determine if the proper user credentials were supplied.

2) Sending an indication to the browser that they've supplied the proper credentials (ie, writing the cookie to the browser)

3) Getting an indication from the browser that they have the proper credentials (ie, reading the cookie from the browser).

Now you can troubleshoot each of these steps, and isolate where the exact problem is.

For 1 above:  make sure you don't have typeing issues, and that your DB is responding 100% correctly.  Meaning, doing a boolean test of ($row = mysql_fetch_row($query)) is scarey to me.  Pull a value explicitly out of the result set, or at least know that you're testing an int, ie:  if( mysql_num_rows($result) === 1 ).  Better yet, do a var_dump() of your final DB result variables (whether they be associative, numeric, etc. arrays) so you know exactly what data you're getting back.

For 2 and 3 above:  when working with cookies, or headers in general, I always use the livehttpheaders plugin for Mozilla.  You can see exactly what is being sent between you and the web server, and can even edit and replay headers to test how your application behaves.  Once you see exactly what you expect being sent back and forth as cookies/headers, you know that piece is working.  If, however the end result expected behavior still isn't what you want, you know it must now be logic in your application that is checking if the user is logged in or not.

Lastly, now use IE :)  If things break, it's a quirk and Google or the php.net user notes will hopefully be able to help you.

And please, try to trim your posts when replying.  Thanks,

H






More information about the talk mailing list