Page 1 of 1

Problem with $_COOKIE

Posted: Tue Mar 15, 2005 4:40 am
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

Posted: Tue Mar 15, 2005 4:46 am
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:

Posted: Tue Mar 15, 2005 5:14 am
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 ?

Posted: Tue Mar 15, 2005 5:31 am
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 ;-)

Posted: Tue Mar 15, 2005 10:39 am
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

Posted: Tue Mar 15, 2005 10:57 am
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....

Posted: Wed Mar 16, 2005 3:30 am
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: