简体   繁体   中英

Undefined index (Cookie)

I have a problem with PHP and hir Cookies. I'm doing an array with cookies.

$possition = 0
setcookie('array['.$possition.']', "test");

echo $_COOKIE['array['.$possition.']'];

With this code, I create a Cookie with name 'array[0]' and with value 'test' (I can see it on Google Chrome cookies management.)

But, when I use the 'echo' appears this message:

Notice: Undefined index: array[0] in C:\xampp\htdocs\recub.php on line 7

But this cookie isn't empty...

Someone know how to solve this? I already tried with "isset" but with this don't show nothing.

Regards

UPDATE:

With Rizier123 works with delay... there is my compleat code:

index.php

<?php
setcookie('array_position', '0');
?>
<html>
<head>
    <style> 
        #lista{border: 1px solid red; height:300px; width: 300px; margin: 0 auto}
        #result{border: 1px solid blue; height:300px; width: 300px; margin: 10 auto}
    </style>
    <script>
        function add(e) {
            var xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
            xmlhttp.open("GET", "cali.php?q=" + e, true);
            xmlhttp.send();
        }
    </script>
</head>
<body>
    <div id="lista">
        <ul>
            <li id="test1" onclick="add('test1')">test1</li>
            <li id="test2" onclick="add('test2')">test2</li>
            <li id="test3" onclick="add('test3')">test3</li>
            <li id="test4" onclick="add('test4')">test4</li>
        </ul>
    </div>
    <div id="result">
        <ul>
            <li id="txtHint"></li>
        </ul>
    </div>
</body>
</html>

cali.php

<?php
// Get element ID.
$q = $_REQUEST["q"];
// Get the current array's position.
$position = $_COOKIE['array_position'];
setcookie('array['.$position.']', $q);
// Assign the Element ID on this Array. (1º Time must be array[0])
$num = $_COOKIE['array_position'];
// Loop for display all array values
for($i=0; $i<=$num; $i++){
    echo $_COOKIE['array'][$i]."</br>";
}
// the current array's position + 1 for next time.
$num2 = $_COOKIE['array_position']+1;
setcookie ('array_position', $num2) ;
?>

(Remember delete the cookies everytime that you reload the page.) With this, the first time that you click an item appear the Undefined index. If you continue clicking items, appears previous item...

You have to change this:

echo $_COOKIE['array['.$possition.']'];

to this:

echo $_COOKIE['array'][$possition];

For more information about setcookie() see the manual: http://php.net/manual/en/function.setcookie.php

And a quote from there (Example 3):

You may also set array cookies by using array notation in the cookie name. This has the effect of setting as many cookies as you have array elements, but when the cookie is received by your script, the values are all placed in an array with the cookie's name:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM