473,246 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

setcookie fails to set cookie!

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
16 11265

"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
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
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

"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
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

"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

"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
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

"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
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
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
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
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

"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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil Powell | last post by:
class LoginSessionGenerator extends MethodGenerator { function LoginSessionGenerator() {} /** * Logout * * @access public */ function &logout() { // STATIC VOID METHOD
5
by: Ben | last post by:
Hi all, In my .php file, I'm using both session_start() and setcookie() before <html> tag. It gives me following warning message: Warning: Cannot modify header information - headers already...
4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
12
by: chrism | last post by:
Hello, I have a pop-up window that I would like to appear in front of the browser home page when a user opens IE. Problem is, I'd like it to never appear again if the user navigates back to the...
5
by: brettr | last post by:
When I reference document.cookie, there is a long string of key=value; pairs listed. I may have 100 hundred cookies on my hard drive. However, most only have one key=value pair. Does the...
1
by: Mike | last post by:
Hello, I can't find any javascript that reads and writes cookies with keys, so that it is compatible with ASP (I want to read and write cookies from both javascript and ASP) for example in...
9
by: Marco Krechting | last post by:
Hi All, I have a page with a list of hyperlinks. I want to save information in a cookie about the fact that I entered an hyperlink or not. When I click one of the hyperlinks I want this stored...
1
by: CR1 | last post by:
I found a great cookie script below, but don't know how to make it also pass the values sent to the cookie, to a querystring as well for tracking purposes. Can anyone help? If there was a way to...
2
by: Ian Collins | last post by:
I'm using PHPUnit to test some PHP scripts and I'd like to know if there is a means of checking which cookies have been set by setcookie() in a function under test. -- Ian Collins.
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.