Note: Undefined index: when calling the set cookie

So, I have cookies that, as I know, are set correctly (using information about the firefox object), and I continue to receive the error / warning message "Note: Undefined index:". I access the cookie using $_COOKIE['username']; , and when I do if(isset($_COOKIE['username'])) , the code does not run. However, I see that the cookie is not displayed in the firefox file. For reference, here is my cookie code: setcookie('username', $username, time()+3600*24);

+4
source share
2 answers

You probably defined the cookie in the php file that was in a different folder of your php file where you called your isset.

Thus, adding '/' as the default folder for a cookie makes it accessible to the entire website.

Sometimes you do not want this to happen because you may want to have two cookies with the same name, but different values โ€‹โ€‹depending on which folder you are in.

Example: A website with 2 languages, you can have $_COOKIE['language'] = 'en'; in the folder /en/ and have $_COOKIE['language'] = 'fr'; in the /fr/ folder.

Therefore, when you set a cookie without specifying its directory, you must remember that it will be available only for files in the same folder or subfolders.

+5
source
 $expire = time()+60*60*24*30; setcookie("MyName", "Khan", $expire,'/'); 
0
source

All Articles