简体   繁体   English

在数组中设置$ _cookie值

[英]Set a $_cookie value in an array

I have a cookie with the following value: 我有一个具有以下值的cookie:

2,3 personen klein,47.50,images/portfolio/portfolio-02.jpg,1|1,2 personen
kleurrijk,47.50,images/portfolio/portfolio-01.jpg,1|3,Sneeuwklokje,47.50,images/portfolio/portfolio-03.jpg,1

I get this value when i use: 我在使用时得到此值:
echo $_cookie['cart'];

The | | is the delimiter for a new line and the , is the delimiter for a new value. 是新行的定界符,而是新值的定界符。

How can I put these values in an array so it becomes useable to echo parts of it, for example when I only want to echo the images. 我如何将这些值放在数组中,以便可以回显部分内容,例如,当我只想回显图像时。 Hope you guys can give me some tips. 希望你们能给我一些提示。

$array = explode("|", $_COOKIE['cart']); 
foraech($array as & $element) {
   $element = explode(",", $element);
}

// example - echo an image
echo $array[1][3]

but to be honest you should keep it in $_SESSION . 但老实说,您应该将其保存在$_SESSION you can store arrays, objects, long strings etc. there and user is not able to change its contents 您可以在此处存储数组,对象,长字符串等,并且用户无法更改其内容

  • Cookies are not good for storing complex data Cookies不利于存储复杂数据

  • Everyone can change their cookies manually and it can lead to security issues 每个人都可以手动更改其Cookie,这可能会导致安全问题

  • There is limit 4096 bytes per one cookie 每个cookie限制为4096个字节

I totally agree with Peter. 我完全同意彼得的看法。

Cookies are simple text files. Cookies是简单的文本文件。 Do not store any Data like the above in Cookies! 请勿在Cookie中存储上述任何数据! You have a nice container with the $_SESSION . 您有一个带有$_SESSION的不错的容器。 It's a global associative array, so you can store values like $_SESSION['cart'] . 这是一个全局关联数组,因此您可以存储$_SESSION['cart']

Put in there whatever u like! 随便放在哪里!

Its more secure than a txt-file on the client's pc! 它比客户端计算机上的txt文件更安全!

Read here about Sessions 在这里阅读有关会议的信息

$string = "2,3 personen klein,47.50,images/portfolio/portfolio-02.jpg,1|1,2 personen kleurrijk,47.50,images/portfolio/portfolio-01.jpg,1|3,Sneeuwklokje,47.50,images/portfolio/portfolio-03.jpg,1";

    $lines = explode('|', $string);

    $lines = array_map(function($a){
        return explode(',', $a);
    }, $lines);

    print_R($lines);

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

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