【问题标题】:How do you set a php cookie with multiple values?如何设置具有多个值的 php cookie?
【发布时间】:2019-09-02 10:09:37
【问题描述】:

我想创建一个 php cookie 来存储用户名和用户 ID。还是只用一个来获得另一个更好?

【问题讨论】:

    标签: php cookies


    【解决方案1】:

    如果您只想存储两个值,将它们连接起来并这样存储可能会更容易:

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

    为了稍后检索信息,

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

    干杯,

    ~狂战士

    【讨论】:

    【解决方案2】:
        <?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

    【讨论】:

    • 这没有回答问题。手册说它“具有设置与数组元素一样多的 cookie 的效果”,而不是具有多个值的单个 cookie。
    【解决方案3】:

    例如可以使用数组

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

    希望这个答案能帮到你

    【讨论】:

    • 迄今为止找到的最佳解决方案。伟大的。谢谢!
    【解决方案4】:

    我知道这是一个旧线程,但这可能会让未来的我有些头疼。我认为最好的方法是序列化/反序列化数据。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2021-05-22
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多