I have this weird Cookie problem. In Firefox with PHP Version 4.3.9 I use setcookie () to set a cookie by the name of "test1". I know the cookie is set because I look in Firefox's list of cookies and there it is. However the following code does not work which has baffled me.

if (isset ($_COOKIE['test1''])) {
   echo "Cookie Set";
}

Any help or info would be appreciated. A look in the PHP manual indicates this should work!

    Hi,

    how does the setcookie line look like ? The code you posted contains an error, there are two single quotes at the end of the string. How does the directory structure look like ?

    Thomas

      The double quotes is my error sorry. The line looks like:

      setcookie ($test, $_POST['option'], 60 + time());
      

      The file that contains the above code is inside a directory inside the webserver. e.g. http://localhost/test/THISFILE.php.

      Thanks

        Did you check the next page load?

        The cookie is not available until the next page load.

          Yeah I did. The weird thing is that the cookie is set but cannot be retrieved. Is it a php.ini thing?

            If the script that checks if the cookie is set is outside of that directory like e.g.

            http://localhost/checkit.php

            then you might need to use

            setcookie ($test, $_POST['option'], 60 + time(),'/');
            

            Add the line

            print_r($_COOKIE);
            

            to check how the _COOKIE array looks like.

            Thomas

              Using print_r I get

              Array ( [test1] => 1 )

              I use a variable called $poll['title'] to obtain the value test1 in a cookie so could it be something to do with that?

                How does the complete code look like ?

                  $polls = mysql_fetch_array (mysql_query ("SELECT title, time, number FROM table"));
                  
                  $title = $polls['title'];
                  $time = $polls['time'] + time ();
                  $number = $polls['number'] + 1;
                  
                  setcookie ($title, $number, $time);
                  

                  Those are the bits of code which directly affect the setcookie function.

                  Also I use:

                  if (isset ($_COOKIE[$poll['title']])) {
                  echo "cookie is set<br><br>";
                  }
                  

                  where I retrieve title from another query.

                    Write a Reply...