开发者

PHP Cookies issues

开发者 https://www.devze.com 2023-03-24 12:02 出处:网络
Hi :) This is very strange -- I am making the call: setcookie(\"ApplicationUserRank\", \"test\", mktime (0, 0, 0, 1, 1, time()+60*60*24*30));

Hi :) This is very strange -- I am making the call:

setcookie("ApplicationUserRank", "test", mktime (0, 0, 0, 1, 1, time()+60*60*24*30));

And then, in another file, calling:

echo $_COOKIE["ApplicationUserRank"];

However, the echo is blank! I am not really sure what I 开发者_如何学JAVAam doing wrong here, anyone care to explain? :)

EDIT: Following Richard's suggestion, I ran the following code:

echo "COOKIE: " . $_COOKIE["ApplicationUserRank"];

Directly after setting the cookie. Still blank! Something seriously strange is happening.

EDIT to my EDIT:

Turns out, that re-runnign the code DOES display the cookie! I guess $_COOKIE does not load from setcookie commands. In fact now that I think about it, I read about that somewhere... Anyways, it would seem that the SAME web file is capable of reading these cookies... Just not any other cookies. (This all is of course after applying many fixes suggested below)


Trusting Tarek's answer, and taking into consideration what rlemon says, it might prove useful to try the functions ob_start() and ob_end_flush(). They are output buffer controlling functions.

As an example:

<?php
// php starts collecting the buffer rather than just sending it all out.
ob_start();

if(!isset($_COOKIE["ApplicationUserRank"])) {
    // This should throw an error about headers already being sent or the cookie wouldn't be set, yet under these circumstances, this will work perfectly.
    echo "Cookie has been set.";
    setcookie("ApplicationUserRank", "new test", mktime (0, 0, 0, 1, 1, 9001));
}
else {
    echo "Cookie is set.";
}

// now it sends the buffer all at once.
ob_end_flush();
?>

ob_start() and ob_end_flush() are also necessary for implementing today's fashions (like encryption) if you have no server-side support for it. Click here for further reading material.

EDIT:

After reading Gumbo's comment and checking PHP mktime's page. Considering 9001 is an invalid year for UNIX, mktime() would always return -1, thus deleting the cookie. A solution would be as follows:

setcookie("ApplicationUserRank", "new test", time()+60*60*24*30); // This should set the expiration date within 30 days from current time.


setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. Cookie values also exist in $_REQUEST.

it is likely that you have something being output to the browser before you set the cookie. This is the only thing I can think of.

See Examples:

<?php
if(!isset($_COOKIE["ApplicationUserRank"]))
{
    setcookie("ApplicationUserRank", "new test", mktime (0, 0, 0, 1, 1, 9001));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<?php
if(isset($_COOKIE["ApplicationUserRank"]))
{
echo    $_COOKIE["ApplicationUserRank"];
}
?>
</body>
</html>

will pass, however

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if(!isset($_COOKIE["ApplicationUserRank"]))
{
    setcookie("ApplicationUserRank", "new test", mktime (0, 0, 0, 1, 1, 9001));
}

if(isset($_COOKIE["ApplicationUserRank"]))
{
echo    $_COOKIE["ApplicationUserRank"];
}
?>
</body>
</html>

will fail


In Chrome or Safari there is a resources tab on the web inspector that'll allow you too see the cookies set. Check that to see if the cookie in fact exists.


Your code is working fine , i tested it and the cookie is being set .

Check to see if you don't change the value of that cookie in another file .

0

精彩评论

暂无评论...
验证码 换一张
取 消