Problem with $_COOKIE

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
schneiderj
Forum Newbie
Posts: 11
Joined: Tue Mar 15, 2005 4:36 am

Problem with $_COOKIE

Post by schneiderj »

Hi,

I have a cookie with the following value :
serveur FALSE / FALSE 1110749626 Produit [N+][Cl-]

and I would like to read this cookie to retrieve the value "[N+][Cl-]". But with the following code :

Code: Select all

$name = $_COOKIEї'Produit'];
        echo $name;
I obtain [N ][Cl-] :?: ... The "+" has been replaced by a space. Any idea would be helpfull.

Jean-Marie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need to add the escaped version to the cookie and unesaspe it to read it.

In PHP the equivalent way of doing it would be

Code: Select all

addslashes('[N+][Cl-]'); //To escape the string

stripslashes($_COOKIE['produit']); //To read the string
:wink:
schneiderj
Forum Newbie
Posts: 11
Joined: Tue Mar 15, 2005 4:36 am

Post by schneiderj »

Thanks for your reply. I try it, but the + is not escaped. I read the doc on this function addslashes, and effectively it seam this sign did not need slashes.
Can I said that a bug of the S_COOKIE ?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

No, try urlencode(); and the urldecode(); to read it back.

I'm too used to using cookies in JavaScript with esacpe() and unescape() sorry I'm getting muddled up.

urlencode('[N+][Cl-]'); then urldecode($_COOKIE['produit']); should certainly work fine ;-)
schneiderj
Forum Newbie
Posts: 11
Joined: Tue Mar 15, 2005 4:36 am

Post by schneiderj »

In fact I write the cookie with JavaScript. But the solution of escape() did not work for the "+".
What I find as solution is to use the JavaScript fonction string. replace(/\+/g,"%2B") :) .

Thanks for your time,
Jean-Marie
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Ah I see. You posted in the PHP forums so I assumed you meant PHP.

Glad you got it sorted.

By the way: PHP has a setcookie() function which does not rely on JavaScript....
schneiderj
Forum Newbie
Posts: 11
Joined: Tue Mar 15, 2005 4:36 am

Post by schneiderj »

Your write !
In fact I post in the PHP forum because I think the problem come from $_COOKIE as my cookie look like fine in my cookie's file :oops:
Post Reply