Web Analytics Made Easy -
StatCounter Cookie not set then Undefined index: vista - CodingForum


No announcement yet.

Cookie not set then Undefined index: vista

  • Filter
  • Time
  • Show
Clear All
new posts

  • Cookie not set then Undefined index: vista


    I have this error message:

    PHP Notice: Undefined index: vista in httpdocs/index.php on line 2

    This is the code:

    $visto = $HTTP_COOKIE_VARS["vista"];

    Whit this code I try to retrieve the values of a cookie and assign it to the variable $visto.

    The error appears only when the cookie is not set.

    When the cookie is set, there is no error message.

    How can i get rid off this error?


  • #2
    Use isset() or the [URL="http://us2.php.net/manual/en/function.array-key-exists.php"]array_key_exists() /URL] function to check a variable to see if it exists before you use it.
    PHP Code:
    if (isset($HTTP_COOKIE_VARS['vista'])) {
    $visto $HTTP_COOKIE_VARS['vista'];
    if (array_key_exists('vista'$HTTP_COOKIE_VARS)) {
    $visto $HTTP_COOKIE_VARS['vista'];


    • #3
      If your code is set to use $visto, then you'll get an error when you try to use it if you do what is suggested above. Consider the below - it initializes $visto and checks for $vista.

      PHP Code:
      $visto = isset($HTTP_COOKIE_VARS['vista']) ? $HTTP_COOKIE_VARS['vista'] : false
      That will elminiate errors for both variables ($visto and 'vista' key). However, you may now have to check if ($visto !== false) in your code before you use it.
      Active PHP/MySQL application developer available for immediate work.
      syosoft.com mavieo.com - Remote Web Site Administration Suite - Reseller Ready