你如何设置一个多个值的PHP cookie?

时间:2011-12-31 21:02:41

标签: php cookies

我想创建一个存储用户名和用户ID的php cookie。或者用一个来获得另一个更好?

4 个答案:

答案 0 :(得分:16)

如果您只想存储两个值,可能更容易连接它们并将其存储为:     

setcookie("acookie", $username . "," . $userid);

稍后检索信息,

if(isset($_COOKIE["acookie"])){
    $pieces = explode(",", $_COOKIE["acookie"]);
    $username = $pieces[0];
    $userid = $pieces[1];
}

干杯,

〜Berserkguard

答案 1 :(得分:4)

    <?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";
    }
}
?>

http://php.net/manual/en/function.setcookie.php

答案 2 :(得分:1)

您可以使用数组,例如

<?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);
}

?>

希望这个答案可以帮助你

答案 3 :(得分:0)

我知道这是一个旧线程,但这可能使将来免于我头疼。我认为最好的方法是对数据进行序列化/反序列化。

setcookie('BlueMonster', serialize($cookiedata);
$arCookie = unserialize($_COOKIE['BlueMonster']);

(从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 '';
  }
?>