问:setcookie函数仅使用字符串来分配值吗?
以下代码中的mycookie方法中的setcookie是否无法使用数组分配值?
<?php
date_default_timezone_set('Asia/Shanghai');
//Variable assignment
$cookie_name = (string)time();
$cookie_value = date('Y-m-d_H:i:s');
$cookie_expire = (time() + 60);
$cookie_path = '/untitled/';
$cookie_domain = '';
$cookie_secure = false;
$cookie_httponly = true;
//Define each array
$MyConfig['Cookie'] = array(
'name' => $cookie_name,
'value' => $cookie_value,
'expire' => $cookie_expire,
'path' => $cookie_path,
'domain' => $cookie_domain,
'secure' => $cookie_secure,
'httponly' => $cookie_httponly,
);
//View the array
foreach ($MyConfig['Cookie'] as $item => $value) {
var_dump($item . var_dump($value));
echo '<br>';
}
//Finally assign a value to cook
function mycookie()
{
setcookie( $MyConfig['Cookie']['name'], $MyConfig['Cookie']['value'],);
//**How can this row be assigned using an array**
}
?>
感谢您告诉我一种方法。
答:如果PHP版本是5.6或更高版本,并且$ myconfig ['cookie']的下标顺序与setcookie的参数顺序一致,setcookie(...$Myconfig['Cookie'])
请参阅PHP手册以使用...运算符进行参数扩展。
答:没有批处理或数组设置方法。如果前端不需要Cookie,则可以对array_进行JSON编码,或者在取出序列时对其进行序列化,以反转操作