0

I save a cookie with value like 131|444|777

And then I try to make an array of it like:

$ausnahmenarray = explode('|', $_COOKIE['ausnahmen']);


foreach($ausnahmearray as $id)
{
    echo $id . '<br>;
}

I don't get anything, so I check the cookie value in the browser and see it's like: 131%7C444%7C777 why the hell can't the value just be stored the way I saved it and how can I read it the way I saved it?

1 Answers1

0

Cookie values are url encoded you just need urldecode:

<?php
setcookie("ausnahmen", "10|20|30");
$ausnahmenarray = explode('|', urldecode($_COOKIE['ausnahmen']));

foreach($ausnahmenarray as $id)
{
    echo $id . '<br>';
}

?>
Peter
  • 1,742
  • 15
  • 26
  • Nop don't work this way, should I save the cookie with urlencode first? –  Dec 13 '17 at 23:30
  • no, cookie values are automatically encoded: `$temp = urldecode("131%7C444%7C777"); echo $temp;` you should see `131|444|777` as an output. you can check it on this site: https://www.tools4noobs.com/online_php_functions/urldecode/ – Peter Dec 13 '17 at 23:33
  • I also see 131|444|777 when I just echo $_COOKIE['ausnahmen'] but the | is not recognicable in explode, Just try it exact the way I I did: store this in cookie and then try to create array out of it, you will see –  Dec 13 '17 at 23:36
  • I changed my code, (it is still the same logic), I tested it and it is works. Use it as a template. You need to run it 2 times because at the 1st time you don't have the cookie value. – Peter Dec 14 '17 at 16:30
  • Ok this code rly works, but the way I needed it didn't work. I solved it by storing/reading as JSON –  Dec 21 '17 at 17:19