访问cookie的数组位置

时间:2015-10-28 10:00:50

标签: php arrays cookies

我正在做一个简单的PHP代码,我发现了一个问题。我想要访问一系列cookie的特定位置的值,但我不知道为什么,我试图使用:

$value = $_COOKIE['conexio[cont]'];

但它不起作用。那是我的代码:

<?php 
    if(!isset($_COOKIE['conexio'])) {
        $valorinicial = 1;
        setcookie('conexio[cont]', $valorinicial);
        setcookie('conexio[data]', date('d-m-y  h:m:s'));
    }
    else {
        $value = $_COOKIE['conexio[cont]'];
        $value = $value +1;
        setcookie('conexio[cont]', $value);
        setcookie('conexio[data]', date('d-m-y  h:m:s'));
    }

 ?>

2 个答案:

答案 0 :(得分:1)

您可以通过读取整个数组来读取数据,然后访问所需的索引:

if(!isset($_COOKIE['conexio'])) {
            $valorinicial = 1;
            setcookie('conexio[cont]', $valorinicial);
            setcookie('conexio[data]', date('d-m-y  h:m:s'));
        }
        else {
            $cookie = $_COOKIE['conexio'];
            $value = $cookie['cont'];
            $value = $value +1;
            setcookie('conexio[cont]', $value);
            setcookie('conexio[data]', date('d-m-y  h:m:s'));
        }

答案 1 :(得分:0)

    //create array for cookie
     $std_arr = array(
                    'index' => 1,
                    'id' => 5,
                    'name' => 'jony',
                    'class' => 'class-8',
                );
    //ready cookie data
     $cookieData = array(
                'cookie_status' => 1,
                'data' => $std_arr
            );
    //create final cookie array
            $cookie = array(
                'name' => 'std_record',
                'value' => json_encode($cookieData),
                'expire' => 86500,
                'secure' => false
            );
//cookie st
            $this->input->set_cookie($cookie);
//cookie data retrive
    $cookie_data_retrive = json_decode(get_cookie('std_record'));
    print_r($cookie_data_retrive->cookie_status);