简体   繁体   English

PHP字符串cookie定义

[英]PHP string cookie define

I have a string defined like: 我有一个定义的字符串:

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

After I place it inside of a cookie its content becomes 将其放入Cookie内后,其内容变为

%2Fportal%2Fimages%2F

I need the string to return like: 我需要像这样返回的字符串:

/portal/images/

I'm kinda combining two answers mentioned here. 我有点结合这里提到的两个答案。

1st 1

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']); 您描述的是默认行为,PHP会自动将其解码为原始值,您无需执行urldecode($_COOKIE['name']);

2nd 第2

You can prevent automatic url encoding by using setrawcookie() 您可以使用setrawcookie()阻止自动url编码

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. 请注意,当您发送cookie时,cookie的值部分将自动进行urlencode,并且在接收到该cookie时,它会被自动解码并通过与cookie名称相同的名称分配给变量。 If you don't want this, you can use setrawcookie() instead if you are using PHP 5. 如果您不想这样做,则可以使用setrawcookie()来代替(如果您使用的是PHP 5)。

Use urldecode when getting cookie value: 获取Cookie值时使用urldecode

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

or 要么

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM