Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-04-2005, 08:44 PM   #1 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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.
metazai is offline   Reply With Quote
Old 08-04-2005, 10:17 PM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 08-05-2005, 01:28 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-05-2005, 08:20 AM   #4 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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?
metazai is offline   Reply With Quote
Old 08-05-2005, 12:45 PM   #5 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 08-05-2005, 12:50 PM   #6 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Good thinking . . . but unfortunately the test did not work.
metazai is offline   Reply With Quote
Old 08-05-2005, 12:57 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 08-05-2005, 07:20 PM   #8 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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"
metazai is offline   Reply With Quote
Old 08-06-2005, 09:17 AM   #9 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 08-06-2005, 12:24 PM   #10 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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.
metazai is offline   Reply With Quote
Old 08-08-2005, 12:41 PM   #11 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
Fascinating . . . I think it's a server time problem. When I take the time/expiring part off, this part:
Code:
time() + 3600
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?
metazai is offline   Reply With Quote
Old 08-08-2005, 01:07 PM   #12 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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.

=+)
metazai is offline   Reply With Quote
Old 08-08-2005, 01:16 PM   #13 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,692
redhead is on a distinguished road
"/" 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..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-08-2005, 01:20 PM   #14 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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.
metazai is offline   Reply With Quote
Old 08-08-2005, 01:21 PM   #15 (permalink)
metazai
Regular Contributor
 
Join Date: Apr 2004
Location: Orange County, CA
Posts: 122
metazai is on a distinguished road
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.
metazai is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
PHP problem with a re-direct googs PHP 8 03-01-2005 04:12 PM
PHP vs .NET Redline Lounge 1 11-24-2004 06:10 AM
php 5 problem Amaranthine PHP 4 06-29-2004 10:25 PM
PHP / JS problem bdl HTML, XML, Javascript, AJAX 2 03-13-2003 08:53 AM


All times are GMT -8. The time now is 07:10 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting