How to use array variables to assign values ​​to the setcookie function in PHP?
PHP中文网
PHP中文网 2017-06-06 09:53:50
0
1
784

Can the setcookie function only use strings for assignment?
Is there no way to use an array to assign a value to setcookie in the mycookie method in the code below? ?

<?php

date_default_timezone_set('Asia/Shanghai');

//变量赋值
$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;

//定义各个数组
$MyConfig['Cookie'] = array(
    'name' => $cookie_name,
    'value' => $cookie_value,
    'expire' => $cookie_expire,
    'path' => $cookie_path,
    'domain' => $cookie_domain,
    'secure' => $cookie_secure,
    'httponly' => $cookie_httponly,
);

//查看数组情况
foreach ($MyConfig['Cookie'] as $item => $value) {
    var_dump($item . var_dump($value));
    echo '<br>';
}

//最终给COOKIE赋值
function mycookie()
{
    setcookie( $MyConfig['Cookie']['name'], $MyConfig['Cookie']['value'],);
    //**这一行如何才能使用数组进行赋值**
}

?>

Thanks for telling me a method.

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
我想大声告诉你

If the PHP version is 5.6 or later, and ensure that the subscript order of $Myconfig['Cookie'] is consistent with the parameter order of setcookie, it can be written as setcookie(...$Myconfig['Cookie'])
See PHP manual: Parameter expansion using... operator

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!