Adam
Adam

Reputation: 20882

PHP Cookies - Stores 2 pieces of data in one cookie

I have 2 pieces of data related to the same topic being country an country code.

eg: Australia and AU

Is there a preferred way to store 2 pieces of data in the one cookie? Or Better to just use 2 cookies.

Sorry if this is a silly questions... Just interested in other peoples opinions.

thx

Upvotes: 0

Views: 263

Answers (4)

Rohit Suthar Mumbai
Rohit Suthar Mumbai

Reputation: 11

You can also set array cookies by using array notation in the cookie name.

PHP Code -

// set the cookies
setcookie("cookie[one]", "cookie1");
setcookie("cookie[two]", "cookie2");
setcookie("cookie[three]", "cookie3");

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

Output:

one : cookie1
two : cookie2
three : cookie3

Upvotes: 0

chris
chris

Reputation: 36937

You can do it a number of ways, either multiple cookies, or storing an Object like JSON, or you could just simply put a string delimited by something like :: for example and user php's eplode function. All in all its a matter of preference and dependent upon what your doing. If you use a JSON object you could easily switch back and forth between the object and an array as well through the use of json_encode and json_decode. Again its all about where your comfort level is. All in all though your looking at working with an array of data in the end if you really want to keep it in a single cookie.

Example:

<?php
//set the cookie
$cookieData = json_encode(array("value_one", "value_two"));
setcookie("MyCookie", $cookieData , time()+3600);

//read the cookie
$cookieInfo = json_decode($_COOKIE['MyCookie']);
echo $cookieInfo[0]."<br>";
echo $cookieInfo[1]."<br>";

//or
for($i = 0; $i < count($cookieInfo); $i++)
{
   echo $cookieInfo[$i]."<br>";
}

You can make this concept fairly large, but bare in mind cookies are easily manipulated client side so sanitize your cookie variables before running them through script that can be injected and broken for other needs, example checking a database to see if a use is valid via cookie information.

Another thing to remember is some browsers have a cookie size, you where the max size of a cookie is 4kb only.

Also you can get a little more fancy with the logic of using JSON and Arrays, they are tricky at first if your not familiar with them but once you start using them and understand them, you will find they are your best friends.

Upvotes: 0

Jashwant
Jashwant

Reputation: 28995

Why not use javascript and create a json object ?

var cc = {
australia: 'au',
India : 'In'
}

You can use as cc.India

I know, you asked about cookies, but I needed to store country codes, and I did this way. Its fast and easy.

Upvotes: 0

Mike Purcell
Mike Purcell

Reputation: 19979

If it were me I would just append the data together using a specific glue, then explode the string when I wanted to separate the values again.

$cookeString = $country . '+' . $countryCode;

list($country, $countryCode) = explode('+', $cookieString);

But in all honesty it doesn't make sense to store country and country code, just store country code and look up country data using country code as a key.

Upvotes: 1

Related Questions