7

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?

4 Answers 4

18

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

1
  • 3
    It seems commas are used to separate different cookies and not allowed in the value: ietf.org/rfc/rfc2109.txt page 9.
    – Sylvain
    Mar 18, 2014 at 5:05
9
    <?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

1
  • 5
    This does not answer the question. Manual says it "has the effect of setting as many cookies as you have array elements", rather than a single cookie with multiple values.
    – redburn
    Jul 18, 2013 at 11:41
2

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

1
  • Best solution found until now. Great. Thanks!
    – Bogdan C
    Apr 2, 2020 at 12:53
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.