1

I have a string defined like:

DEFINE('IMAGES_DIR',"/portal/images/");

After I place it inside of a cookie its content becomes

%2Fportal%2Fimages%2F

I need the string to return like:

/portal/images/
1
  • use setrawcookie or urldecode it later Commented Aug 9, 2013 at 23:10

2 Answers 2

1

I'm kinda combining two answers mentioned here.

1st

what you described is the default behaviour, PHP will automatically decode it to its original value, you don't need to do urldecode($_COOKIE['name']);

2nd

You can prevent automatic url encoding by using setrawcookie()

Docs

Note that the value portion of the cookie will automatically be urlencoded when you send the cookie, and when it is received, it is automatically decoded and assigned to a variable by the same name as the cookie name. If you don't want this, you can use setrawcookie() instead if you are using PHP 5.

Sign up to request clarification or add additional context in comments.

1 Comment

Your "1st" answer is inconsistent with the very docs you quoted: "when it is received, it is automatically decoded"
0

Use urldecode when getting cookie value:

echo urldecode('%2Fportal%2Fimages%2F');

or

//for cookie
echo urldecode($_COOKIE['IMAGES_DIR']);

//for your example above with the contant
echo urldecode(IMAGES_DIR);

2 Comments

Thank you! the urldecode set me on the right path, i am using javascript to parse the cookie contents :) thank you! ecodeURIComponent()
You shouldn't need to decode it. OP must be fetching the cookie by means other than $_COOKIE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.