By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

setcookie fails to set cookie!

P: n/a
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE['nordicnet_registration'] and there is no value there! Guys, what
on earth do I do about this????

Here is the code that sets the cookie:

if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration', $row['nnet_user_registrationnumber'],
0, '/');
@mysql_free_result($query);
mysql_close($dbConn) or die('Could not close db');
$html .= $font . '<font color=000099>Takk for logging inn. For forsatte
' .
"<a
href=http://$serverName/index.php?content=membersites/Palogget%20S1.php " .
' target=_top>klikk her</a></font></font>';
} else if (sizeof($_POST) > 0) {
$errorMsg .= $font . '<font color=cc0000><li>Det finns en problem med
db</li></font></font><p>';
$hasLoggedIn = 0;
}
Because the PHP script is in a frame, instead of using header() I print out
some text with a link <a href=mylink.php target=_top> for the user to click
and to go to another page. By then the cookie should be registered, yet
it's not there!

I'm at a complete loss.

Phil
Jul 16 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a

"Phil Powell" <so*****@erols.com> schreef in bericht
news:Ikq6b.126616$xf.18927@lakeread04...
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE['nordicnet_registration'] and there is no value there! Guys, what on earth do I do about this????

Here is the code that sets the cookie:

if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration', $row['nnet_user_registrationnumber'], 0, '/');


Makes sence, because you are giving the cookie a life time of 0 seconds. If
you want to create a session cookie with the path argument (so, without the
expire argument), try using the header() function instead:

header("Set-Cookie:
nordicnet_registration={$row['nnet_user_registrationnumber}; path=/");

Be aware that cookie values should be url encoded when they contain special
characters like whitespaces.
JW

Jul 16 '05 #2

P: n/a
Ok I've had it! PHP is losing its superiority status at this point! I am
beginning to HATE this language!!!!!!!!

I did what you suggested and it worked.. but why is it that in logginn.php
your header() solution worked, but it FAILED it registrering.php to do the
EXACT SAME THING! However, THIS worked in registrering.php but FAILED in
logginn.php:

setcookie('nordicnet_registration', $registrationNumber, 0, '/');

Both have the exact same logic: set cookie -> display text with link -> go
to next page

Phil
"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...

"Phil Powell" <so*****@erols.com> schreef in bericht
news:Ikq6b.126616$xf.18927@lakeread04...
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE['nordicnet_registration'] and there is no value there! Guys, what
on earth do I do about this????

Here is the code that sets the cookie:

if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration',

$row['nnet_user_registrationnumber'],
0, '/');


Makes sence, because you are giving the cookie a life time of 0 seconds.

If you want to create a session cookie with the path argument (so, without the expire argument), try using the header() function instead:

header("Set-Cookie:
nordicnet_registration={$row['nnet_user_registrationnumber}; path=/");

Be aware that cookie values should be url encoded when they contain special characters like whitespaces.
JW

Jul 16 '05 #3

P: n/a
Phil Powell wrote:
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE['nordicnet_registration'] and there is no value there! Guys, what
on earth do I do about this????

Here is the code that sets the cookie:

if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration', $row['nnet_user_registrationnumber'],
0, '/'); [...]By then the cookie should be registered, yet it's not there!

I'm at a complete loss.


mysql_fetch_row() does not return the column names as an index!

try mysql_fetch_assoc()
or use numeric indexes for the colunms:
setcookie('nordicnet_registration', $row[0], 0, '/');
HTH

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #4

P: n/a

"Phil Powell" <so*****@erols.com> schreef in bericht
news:AIq6b.126697$xf.39169@lakeread04...
Ok I've had it! PHP is losing its superiority status at this point! I am
beginning to HATE this language!!!!!!!!

PHP, nor any other language can teach the programmer the proper and logical
way of thinking. When you know that the third argument of a function that
creates a cookie sets its life time, you must realize that you should pass
it a value larger then zero to create it.
I did what you suggested and it worked.. but why is it that in logginn.php
your header() solution worked, but it FAILED it registrering.php to do the
EXACT SAME THING! However, THIS worked in registrering.php but FAILED in
logginn.php:

setcookie('nordicnet_registration', $registrationNumber, 0, '/');


Then you are probably facing a conflict with cookie names which overwrite
each other. Try other names to test this, eg:

header("Set-Cookie: nordicnet_registration=1; path=/");

in registering.php and

header("Set-Cookie: nordicnet_registration2=1; path=/");

in login.php.

Also, don't forget that once a cookie has been created, it will only be
available after a reload of the page.

So:

<?
header("Set-Cookie: nordicnet_registration=1; path=/");
echo $_COOKIE['nordictnet_registration'];
?>

Displays nothing.
JW

Jul 16 '05 #5

P: n/a
OK this is how I did my test:

1) I go to registrering.php and register fully, thereby setting the
nordicnet_registration cookie. It's all set, everything is fine (uses
setcookie() with 0 time).

2) I close my browser

3) I reboot computer (just for safety sake)

4) I reopen my browser and go to logginn.php

5) I log in, however, setcookie() this time does NOT work, the cookie is not
set because AFTER I click the link to go to another page the
$_COOKIE['nordicnet_registration'] value is empty, thus, no cookie. HOWEVER,
header() DOES work in logginn.php!

6) I change the code in registrering.php to do header() instead of
setcookie() (remember: setcookie() works, header() does NOT work in
registrering.php)

7) I close the browser

8) I reboot computer

9) I reopen browser

10) I go to registrering.php Now THIS TIME I have header() and NOT
setcookie() instead of the reverse. I register under a new name and email..
Registration is complete, the cookie SHOULD be set. I get a link to click.
I click the link, go to my next page.. AND NO COOKIE!

So.. this is what happens:

registrering.php:

Register -> setcookie('stuff', $stuff, 0, '/') WORKS -> click link -> there
is the cookie!

Register -> header() FAILS -> click link -> NO COOKIE

logginn.php:

Login -> setcookie('stuff', $stuff, 0, '/') FAILS -> click link -> NO COOKIE

Login -> header() WORKS -> click link -> there is the cookie!

Phil
"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...

"Phil Powell" <so*****@erols.com> schreef in bericht
news:AIq6b.126697$xf.39169@lakeread04...
Ok I've had it! PHP is losing its superiority status at this point! I am beginning to HATE this language!!!!!!!!

PHP, nor any other language can teach the programmer the proper and

logical way of thinking. When you know that the third argument of a function that
creates a cookie sets its life time, you must realize that you should pass
it a value larger then zero to create it.
I did what you suggested and it worked.. but why is it that in logginn.php your header() solution worked, but it FAILED it registrering.php to do the EXACT SAME THING! However, THIS worked in registrering.php but FAILED in
logginn.php:

setcookie('nordicnet_registration', $registrationNumber, 0, '/');


Then you are probably facing a conflict with cookie names which overwrite
each other. Try other names to test this, eg:

header("Set-Cookie: nordicnet_registration=1; path=/");

in registering.php and

header("Set-Cookie: nordicnet_registration2=1; path=/");

in login.php.

Also, don't forget that once a cookie has been created, it will only be
available after a reload of the page.

So:

<?
header("Set-Cookie: nordicnet_registration=1; path=/");
echo $_COOKIE['nordictnet_registration'];
?>

Displays nothing.
JW

Jul 16 '05 #6

P: n/a

"Pedro" <he****@hotpop.com> schreef in bericht
news:hf********************************@4ax.com...

mysql_fetch_row() does not return the column names as an index!

try mysql_fetch_assoc()
or use numeric indexes for the colunms:
setcookie('nordicnet_registration', $row[0], 0, '/');


Yes, I think this indeed caused the problem. It seems that when the cookie
function is applied with 0 as the expire time, it does the same as the
header call I suggested.
JW


Jul 16 '05 #7

P: n/a

"Phil Powell" <so*****@erols.com> schreef in bericht
news:drr6b.126857$xf.1324@lakeread04...
OK this is how I did my test:


I think that I have taken you to the wrong track. Leave the setcookie()
calls as they are and take a look at Pedro's suggestion.
JW

Jul 16 '05 #8

P: n/a
On Sat, 6 Sep 2003 22:05:32 +0200, "Janwillem Borleffs" <jw*@jwbfoto.demon.nl>
wrote:
PHP, nor any other language can teach the programmer the proper and logical
way of thinking. When you know that the third argument of a function that
creates a cookie sets its life time, you must realize that you should pass
it a value larger then zero to create it.


It is stated in the manual that specifying zero for this argument means to
skip it, i.e. not set an expiry time, for the case when you want to use the
other parameters after 'expire'.

"
All the arguments except the name argument are optional. You may also replace
an argument with an empty string ("") in order to skip that argument. Because
the expire and secure arguments are integers, they cannot be skipped with an
empty string, use a zero (0) instead. The following table explains each
parameter of the setcookie() function, be sure to read the Netscape cookie
specification for specifics on how each setcookie() parameter works and RFC
2965 for additional information on how HTTP cookies work.
"

"
expire The time the cookie expires. This is a unix timestamp so is in number of
seconds since the epoch. In otherwords, you'll most likely set this with the
time() function plus the number of seconds before you want it to expire. Or you
might use mktime().

time()+60*60*24*30 will set the cookie to expire in 30 days. If not set, the
cookie will expire at the end of the session (when the browser closes).
"

So zero does mean 'session cookie'.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #9

P: n/a

"Andy Hassall" <an**@andyh.co.uk> schreef in bericht
news:ct********************************@4ax.com...

So zero does mean 'session cookie'.


Yes, now I know...

JW

Jul 16 '05 #10

P: n/a
Ok I changed my code to reflect the associative array $row:

// IF hasLoggedIn IS STILL TRUE WE FOUND THEIR LOGIN INFORMATION - CLOSE UP,
SET COOKIE AND REDIRECT
if ($hasLoggedIn && ($row = mysql_fetch_assoc($query))) {
setcookie('nordicnet_registration',
"$row['nnet_user_registrationnumber']", 0, '/');
//header("Set-Cookie:nordicnet_registration={" .
$row['nnet_user_registrationnumber'] . "}; path=/");
@mysql_free_result($query);
mysql_close($dbConn) or die('Could not close db');
$html .= $font . '<font color=000099>Takk for logging inn. For forsatte
' .
"<a href=/membersites/Palogget%20S1.php target=_top>klikk
her</a></font></font>";
} else if (sizeof($_POST) > 0) {
$errorMsg .= $font . '<font color=cc0000><li>Det finns en problem med
db</li></font></font><p>';
$hasLoggedIn = 0;
}

Guess what? IT STILL FAILED! Unless I specfically use header() instead of
setcookie() it never works within logginn.php (but if I use setcookie()
instead of header() in registrering.php with the exact same program logic,
it works, the opposite fails!)

Phil
"Pedro" <he****@hotpop.com> wrote in message
news:hf********************************@4ax.com...
Phil Powell wrote:
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE['nordicnet_registration'] and there is no value there! Guys, whaton earth do I do about this????

Here is the code that sets the cookie:

if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration', $row['nnet_user_registrationnumber'],0, '/');

[...]
By then the cookie should be registered, yet it's not there!

I'm at a complete loss.


mysql_fetch_row() does not return the column names as an index!

try mysql_fetch_assoc()
or use numeric indexes for the colunms:
setcookie('nordicnet_registration', $row[0], 0, '/');
HTH

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:

Jul 16 '05 #11

P: n/a
Ok last time.. I changed to $row[0] and everything is fine with setcookie()
now..

So apparently while header() could set an undefined cookie value,
setcookie() will not do so. Is there documentation on this behavior?

Phil

"Phil Powell" <so*****@erols.com> wrote in message
news:ZBr6b.126900$xf.85727@lakeread04...
Ok I changed my code to reflect the associative array $row:

// IF hasLoggedIn IS STILL TRUE WE FOUND THEIR LOGIN INFORMATION - CLOSE UP, SET COOKIE AND REDIRECT
if ($hasLoggedIn && ($row = mysql_fetch_assoc($query))) {
setcookie('nordicnet_registration',
"$row['nnet_user_registrationnumber']", 0, '/');
//header("Set-Cookie:nordicnet_registration={" .
$row['nnet_user_registrationnumber'] . "}; path=/");
@mysql_free_result($query);
mysql_close($dbConn) or die('Could not close db');
$html .= $font . '<font color=000099>Takk for logging inn. For forsatte ' .
"<a href=/membersites/Palogget%20S1.php target=_top>klikk
her</a></font></font>";
} else if (sizeof($_POST) > 0) {
$errorMsg .= $font . '<font color=cc0000><li>Det finns en problem med
db</li></font></font><p>';
$hasLoggedIn = 0;
}

Guess what? IT STILL FAILED! Unless I specfically use header() instead of
setcookie() it never works within logginn.php (but if I use setcookie()
instead of header() in registrering.php with the exact same program logic,
it works, the opposite fails!)

Phil
"Pedro" <he****@hotpop.com> wrote in message
news:hf********************************@4ax.com...
Phil Powell wrote:
Fourth attempt.. it fails now in login, I check by printing
$_COOKIE['nordicnet_registration'] and there is no value there! Guys, whaton earth do I do about this????

Here is the code that sets the cookie:

if ($hasLoggedIn && ($row = mysql_fetch_row($query))) {
setcookie('nordicnet_registration', $row['nnet_user_registrationnumber'],0, '/');

[...]
By then the cookie should be registered, yet it's not there!

I'm at a complete loss.


mysql_fetch_row() does not return the column names as an index!

try mysql_fetch_assoc()
or use numeric indexes for the colunms:
setcookie('nordicnet_registration', $row[0], 0, '/');
HTH

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:


Jul 16 '05 #12

P: n/a
Phil Powell wrote:
Ok I changed my code to reflect the associative array $row:

// IF hasLoggedIn IS STILL TRUE WE FOUND THEIR LOGIN INFORMATION - CLOSE UP,
SET COOKIE AND REDIRECT
if ($hasLoggedIn && ($row = mysql_fetch_assoc($query))) {
setcookie('nordicnet_registration',
"$row['nnet_user_registrationnumber']", 0, '/');


?????

"$row['xyz']" is not the same as
$row['xyz'] and not the same as
"$row[xyz]"

as far as I can tell only the last two work :)

--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #13

P: n/a
Phil Powell wrote:
Ok last time.. I changed to $row[0] and everything is fine with setcookie()
now..

So apparently while header() could set an undefined cookie value,
setcookie() will not do so. Is there documentation on this behavior?


I even tried to make sense of RFC2965 [1] ... but RFCs, definitely,
are not my friends.

[1] http://www.faqs.org/rfcs/rfc2965
--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #14

P: n/a

"Phil Powell" <so*****@erols.com> schreef in bericht
news:HHr6b.126921$xf.3928@lakeread04...
Ok last time.. I changed to $row[0] and everything is fine with setcookie() now..

So apparently while header() could set an undefined cookie value,
setcookie() will not do so. Is there documentation on this behavior?


I performed some tests:

When you call
setcookie("test","",0,"/");

The cookie header looks like this:
Set-Cookie: test=deleted; expires=Fri, 06-Sep-02 21:08:27 GMT; path=/

Which deletes the cookie immediately.

But when you create the cookie through the header function with:
header("Set-Cookie: test=; path=/");

The cookie header looks like:
Set-Cookie: test=; path=/

So the cookie is indeed created with an empty value. I think this is an
undocumented feature of the setcookie() function (group, please correct me
if I'm wrong).
JW

Jul 16 '05 #15

P: n/a
Janwillem Borleffs wrote:

I think this is an
undocumented feature of the setcookie() function (group, please correct me
if I'm wrong).


http://www.php.net/setcookie (for some reason not working right now
http://www.php.net/manual/en/function.setcookie.php

<copypaste>
Cookies must be deleted with the same parameters as they were set
with. If the value argument is an empty string (""), and all other
arguments match a previous call to setcookie, then the cookie with the
specified name will be deleted from the remote client.
</copypaste>
--
"Yes, I'm positive."
"Are you sure?"
"Help, somebody has stolen one of my electrons!"
Two atoms are talking:
Jul 16 '05 #16

P: n/a

"Pedro" <he****@hotpop.com> schreef in bericht
news:8i********************************@4ax.com...

<copypaste>
Cookies must be deleted with the same parameters as they were set
with. If the value argument is an empty string (""), and all other
arguments match a previous call to setcookie, then the cookie with the
specified name will be deleted from the remote client.
</copypaste>


Doesn't match the bill entirely, because the behaviour also occurs when
there aren't previous calls to setcookie.

JW

Jul 16 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.