Jump to content

Recommended Posts

I am reading a ton of information on cookies, as I am looking to implement a cookie on my website.  However, I cannot find any articles on assigning more than one variable to a cookie.  Everything I read is of the following format:

setcookie("name", "value", "expiration", "path", "domain", "security");

So it seems that the cookie can only take one true value field.

I am looking to store more than one "value".  For example, if I was looking to store a username, pageviews, and an e-mail address, would I have to set three individual cookies?  This doesn't sound right at all, so I am sure that I am not understanding something.  Any input would be of the greatest assistance ...
Link to comment
https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/
Share on other sites

No, you can store pretty much as many values as you want.

For example:

setcookie("name1", "value1", "expiration");
setcookie("name2", "value2", "expiration");
setcookie("name3", "value3", "expiration");

Or you can set an array like this:

setcookie("cookie[three]", "cookiethree");
setcookie("cookie[two]", "cookietwo");
setcookie("cookie[one]", "cookieone");

Or you can use serialize to create a single "object" of an array to store it and then deserialize it when you read it.

[b]EDIT: When setting multiple values to a cookie (as in the 1st and 2nd examples), they are not stored in separate cookies, they are all saved within the same cookie.[/b]
a good idea would be to make an array of that information and then
[url=http://www.php.net/serialize]serialize()[/url]
it

example
[code]
$info = array();
$info['username'] = "zanus";
$info['pageviews'] = 74;
$info['email'] = "blah@blah.com";

setcookie("uncookie", serialize($info), time()+(60*60*24));

then call it like

$someinfo = unserialize($_COOKIE['uncookie']);
print_r($someinfo, true);
[/code]
Thanks guys.  I think I have it figured out.  BTW - if you use serialize() you create a security problem (so says php.net; check out their section on cookies).  Right now it seems I am getting ahead of myself.

I had my script working just fine, then I switched hosts and now my script won't work at all.  I have the following form:

[code]
<form method="post" action="index2.php">
<p>Server URL:  <input type="text" name="serverurl" size="30" maxlength="30"></p>
<p>Database Name:  <input type="text" name="databasename" size="30" maxlength="30"></p>
<p>Database Username:  <input type="text" name="nameofuser" size="30" maxlength="30"></p>
<p>Database Password:  <input type="password" name="passofuser" size="30" maxlength="30"></p>
<input type="submit" value="Submit">
</form>
[/code]

The damn thing won't pass the variables to index2.php.  I cannot understand it.  I have been told that variable tracking is turned on at all times on the php version installed on this server, and their documentation states the same.  But I still cannot get the variables to show.

I have tried the following (index2.php):

[code]
echo "$serverurl<br>";
echo "$databasename<br>";
echo "$nameofuser<br>";
echo "$passofuser<br>":
[/code]

The result are four blank lines.



This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.