|
 |
|
 |
08-04-2005, 08:44 PM
|
#1 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
PHP Cookie problem
I must be missing something. Can anybody see why this isn't displaying the results of the cookie, and is not in fact seemingly created at all:
Code:
<?php
if(isset($_COOKIE['cart_id'])) {
$cart_id = $_COOKIE['cart_id'];
} else {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 3600);
}
echo ($_COOKIE['cart_id']); ?>
I'm stumped.
|
|
|
08-04-2005, 10:17 PM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
When I ran this is firefox as
Code:
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<?php
if(isset($_COOKIE['cart_id'])) {
$cart_id = $_COOKIE['cart_id'];
} else {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 3600);
}
echo ($_COOKIE['cart_id']); ?>
</body>
</html>
I got an error form firefox saying the headers had already been set and thus the cookie couldn't be added. But after changing it to
Code:
<?php
if(isset($_COOKIE['cart_id'])) {
$cart_id = $_COOKIE['cart_id'];
} else {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 3600);
}
<html>
<head>
<title></title>
<meta content="">
<style></style>
</head>
<body>
<?PHP
echo ($_COOKIE['cart_id']); ?>
</body>
</html>
It worked fine.
__________________
Stop intellectual property from infringing on me
|
|
|
08-05-2005, 01:28 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
|
The issue here is, that cookies must be set, befor any additional header is beeing send to the browser, it's even mentioned in teh description of setcookie()
Quote:
|
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 <html> and <head> tags as well as any whitespace. If output exists prior to calling this function, setcookie() will fail and return FALSE. If setcookie() successfully runs, it will return TRUE. This does not indicate whether the user accepted the cookie.
|
|
|
|
08-05-2005, 08:20 AM
|
#4 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Then is it a server issue?
I apologize for not being more specific. The cookie IS set before the HTML tag. This is how it has always been set, but on multiple computers I'm still not getting an output. I have just discovered, however, that it might be a server issue, as when I put it on another php-enabled server the cookie IS set.
The server info can be found at:
http://www.aplaceforbaby.com/newsite/test.php
Not maintaining the server myself, is there something I need to tell the system administrator to turn on/off?
|
|
|
08-05-2005, 12:45 PM
|
#5 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
I'm just guessing here so don't take this too seriously, but there is something in your server info called turck_mmcache which might be caching the pae on the first run through then giving you a static page on refresh, but I wouldn't know for sure. One way to test for that would be to make the page that sets the cookie provide a link to a page that reads the cookie. If the cookie is successfully read from the second page then it's most likely a caching problem.
__________________
Stop intellectual property from infringing on me
|
|
|
08-05-2005, 12:50 PM
|
#6 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Good thinking . . . but unfortunately the test did not work.
|
|
|
08-05-2005, 12:57 PM
|
#7 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
|
Try snagging and displaying the return value of the setcookie() function. If it's false your cookie isn't being set, but if it's true your browser is refusing it.
__________________
Stop intellectual property from infringing on me
|
|
|
08-05-2005, 07:20 PM
|
#8 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Do you mean something like
Code:
if(setcookie())
{
echo "Cookie set";
}
else
{
echo "Cookie not set";
}
If so, it's coming up FALSE, or "Cookie not set"
|
|
|
08-06-2005, 09:17 AM
|
#9 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
You'll need to specify some options for setcookie(). It wont work like that.
Code:
<?php
if(setcookie('foo','bar', time() + 3600)) {
echo "Cookie set";
}
else {
echo "Cookie not set";
}
?>
Both the above and your original snippet work for me. (php 4.3.11 here)
-r
|
|
|
08-06-2005, 12:24 PM
|
#10 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Argh Argh Argh Argh Argh. Ok, the last code from idx showed that a cookie was being set, and everything appears to work fine -- in Firefox. My IE, however, isn't accepting the cookie. I have my Internet security settings set to Accept ALL Cookies, however, so I'm not sure why this would be.
|
|
|
08-08-2005, 12:41 PM
|
#11 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Fascinating . . . I think it's a server time problem. When I take the time/expiring part off, this part: It works fine in both browsers. Which is a problem, as I do want the cookie to expire in an hour. Can you set a cookie with time from the client machine? Or is that just asking for trouble?
|
|
|
08-08-2005, 01:07 PM
|
#12 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Well, I solved it, and for the life of me I don't know why this is, but I found it in another forum on the web. Check it out . . . if you're having problems with IE figuring out the time settings and expiration on your cookie, add a ,"/". in the code. So this is what I have now that works.
Code:
if(isset($_COOKIE['cart_id'])) {
$cart_id = $_COOKIE['cart_id'];
} else {
$cart_id = md5(uniqid(rand()));
setcookie("cart_id", $cart_id, time() + 3600, "/");
}
Why? Why NOT?
I hate computers.
=+)
|
|
|
08-08-2005, 01:16 PM
|
#13 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
|
"/" is telling the server, that the cookie is to have effect from the root and onwards into any subdirectory on the server.
I wouldn't know why that would affect the cookies duration time wise when dealing with client/server timezones..
It could be a IE bug..
|
|
|
08-08-2005, 01:20 PM
|
#14 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Interesting . . . still, it works now, with only that change. Could it be some setting in PHP that needs the times on the server and client to be synced? (the client time seems to be an hour ahead of me). Probably not, I'm just grasping at straws. Just hope it KEEPS working.
|
|
|
08-08-2005, 01:21 PM
|
#15 (permalink)
|
|
Regular Contributor
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
|
Guess what. It stopped working. I was reading a cookie set when I had taken the time off earlier in experimentation. I will now officially break down crying, or possibly go insane.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:10 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|