Hello. If you open a website, the first thing you do is execute a code that writes a specific value in a cookie:

setcookie('param', '1', time() + 60 * 60 * 24 * 7, "/"); 

And on the same page, I display the recorded value:

 echo $_COOKIE['param']; 

And if you go to this page for the first time, you get a notification:

Notice: Undefined index: param

After updating the page, everything works as it should. This is due to the fact that in cookies, when you first start the page there is no param parameter.

how can this be solved?

  • And what do you want to achieve at all? Well, if it is not there yet, but you definitely need to perform some kind of operation - then do it - Alexey Shimansky
  • @ AlekseyShimansky I need to check if the user has a record in paras with param = 1, if yes, execute the code, if not, do not execute the code. the same notification is issued during the check - iKey
  • one
    Well, if (isset($_COOKIE['param'])) { // do smth } - Alexey Shimansky
  • @ Alexey Shimansky, so I’m doing this, and I’m getting Notice: Undefined index: param - iKey
  • I am isset somewhere lost. We need through him - Alexey Shimansky

1 answer 1

Usually, to circumvent this situation, they simply check for the existence of a variable, and only access it if it exists.

 if(isset($_COOKIE['param'])) echo $_COOKIE['param']; 
  • exactly what is needed. thanks - iKey