I was doing a simple php code and I found a problem. I would want to access to the value of a specific position of a array of cookies, but I don't know why, I tried to use:
$value = $_COOKIE['conexio[cont]'];
But It doesn't works. That's my code:
<?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'));
}
?>
You can read the data by reading the whole array and access the desired index afterwards:
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'));
}
//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);