Charles Murray
Charles Murray

Reputation: 383

How do you set a php cookie with multiple values?

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?

Upvotes: 7

Views: 32928

Answers (4)

Beeker
Beeker

Reputation: 405

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']);

(hijacked from https://www.brainbell.com/tutorials/php/Saving_Multiple_Data_In_One_Cookie.htm since it was far more complete than I would have thrown together in 5 mins)

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

Upvotes: 2

Virtuoso NoShow
Virtuoso NoShow

Reputation: 71

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

Upvotes: 2

berserkguard
berserkguard

Reputation: 335

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

Upvotes: 18

andrewk
andrewk

Reputation: 3871

    <?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

Upvotes: 9

Related Questions