如何设置具有多个值的 php cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8690537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set a php cookie with multiple values?
提问by Charles Murray
I want to create a php cookie that stores the username and the userid. Or is it better to just use one to get the other?
我想创建一个存储用户名和用户 ID 的 php cookie。还是只用一个来获得另一个更好?
回答by berserkguard
If you're only looking to store two values, it may just be easier to concatenate them and store it as such:
如果您只想存储两个值,将它们连接起来并按如下方式存储可能会更容易:
setcookie("acookie", $username . "," . $userid);
And to retrieve the information later,
并在以后检索信息,
if(isset($_COOKIE["acookie"])){
$pieces = explode(",", $_COOKIE["acookie"]);
$username = $pieces[0];
$userid = $pieces[1];
}
Cheers,
干杯,
~Berserkguard
~狂战士
回答by andrewk
<?php
// set the cookies
setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");
// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
foreach ($_COOKIE['cookie'] as $name => $value) {
$name = htmlspecialchars($name);
$value = htmlspecialchars($value);
echo "$name : $value <br />\n";
}
}
?>
回答by Virtuoso NoShow
You can use an array for example
例如,您可以使用数组
<?php
// the array that will be used
// in the example
$array = array(
'name1' => 'value1',
'name2' => 'value2',
'name3' => 'value3'
);
// build the cookie from an array into
// one single string
function build_cookie($var_array) {
$out = '';
if (is_array($var_array)) {
foreach ($var_array as $index => $data) {
$out .= ($data != "") ? $index . "=" . $data . "|" : "";
}
}
return rtrim($out, "|");
}
// make the func to break the cookie
// down into an array
function break_cookie($cookie_string) {
$array = explode("|", $cookie_string);
foreach ($array as $i => $stuff) {
$stuff = explode("=", $stuff);
$array[$stuff[0]] = $stuff[1];
unset($array[$i]);
}
return $array;
}
// then set the cookie once the array
// has been through build_cookie func
$cookie_value = build_cookie($array);
setcookie('cookie_name', $cookie_value, time() + (86400 * 30), "/");
// get array from cookie by using the
// break_cookie func
if (isset($_COOKIE['cookie_name'])) {
$new_array = break_cookie($_COOKIE['cookie_name']);
var_dump($new_array);
}
?>
Hope this answer help you out
希望这个回答能帮到你
回答by Aaron Baxter
I know it's an old thread, but this might save a future me some headache. The best way I believe would be to serialize/unserialize the data.
我知道这是一个旧线程,但这可能会让未来的我头疼。我认为最好的方法是序列化/反序列化数据。
setcookie('BlueMonster', serialize($cookiedata);
$arCookie = unserialize($_COOKIE['BlueMonster']);
(hiHymaned from https://www.brainbell.com/tutorials/php/Saving_Multiple_Data_In_One_Cookie.htmsince it was far more complete than I would have thrown together in 5 mins)
(从https://www.brainbell.com/tutorials/php/Saving_Multiple_Data_In_One_Cookie.htm劫持,因为它比我在 5 分钟内拼凑的更完整)
<?php
require_once 'stripCookieSlashes.inc.php';
function setCookieData($arr) {
$cookiedata = getAllCookieData();
if ($cookiedata == null) {
$cookiedata = array();
}
foreach ($arr as $name => $value) {
$cookiedata[$name] = $value;
}
setcookie('cookiedata',
serialize($cookiedata),
time() + 30*24*60*60);
}
function getAllCookieData() {
if (isset($_COOKIE['cookiedata'])) {
$formdata = $_COOKIE['cookiedata'];
if ($formdata != '') {
return unserialize($formdata);
} else {
return array();
}
} else {
return null;
}
}
function getCookieData($name) {
$cookiedata = getAllCookieData();
if ($cookiedata != null &&
isset($cookiedata[$name])) {
return $cookiedata[$name];
}
}
return '';
}
?>