0

I am trying to store some values via array in cookies using JavaScript, so that later I can call and use them in PHP variables. However, when I check the cookies, the array I stored has no name in the cookies, hence I cannot access them. Cookie name, appears as blank. This is how I am setting the cookie.

<script>
cook = {name: "john",email:"john@live.co.uk"};      
mycook = JSON.stringify(cook); 
document.cookie = mycook;
</script>

This is how it appears in inspect elements.

No Cookie name

To call the values in PHP, I used the following code, but none of them work. I just get undefined array key error message. I used 2 variations to see which of them works to echo the cookie elements, but none of them work.

<?php
if(isset($_COOKIE)){
  echo $_COOKIE['mycook.name'];
  echo '<br>';
  echo $_COOKIE['mycook->email'];  
}
?> 
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
m_imran79
  • 9
  • 2
  • you can set name like : cook = {name: "john",email:"john@live.co.uk"}; mycook = JSON.stringify(cook); document.cookie = 'setdata' + "=" + mycook +""; – Lalit Kumar Apr 07 '23 at 06:16
  • I am voting to reopen because the duplicate link is about using javascript to create the cookie and to read it. Whereas this is about using javascript to create it but PHP to read it. The error may be in the reading code. – Rohit Gupta Apr 09 '23 at 13:36

0 Answers0