xProteuSx Posted November 30, 2006 Share Posted November 30, 2006 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/ Share on other sites More sharing options...
taith Posted November 30, 2006 Share Posted November 30, 2006 yes, you would need to set 3 seperate cookies... unfortuanatly .php cannot set an array as a cookie :-( Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/#findComment-133051 Share on other sites More sharing options...
Psycho Posted November 30, 2006 Share Posted November 30, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/#findComment-133054 Share on other sites More sharing options...
Zane Posted November 30, 2006 Share Posted November 30, 2006 a good idea would be to make an array of that information and then[url=http://www.php.net/serialize]serialize()[/url]itexample[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] Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/#findComment-133057 Share on other sites More sharing options...
xProteuSx Posted November 30, 2006 Author Share Posted November 30, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/#findComment-133062 Share on other sites More sharing options...
Zane Posted November 30, 2006 Share Posted November 30, 2006 you need to grab them from the POST array like so$_POST['serverurl']$_POST['databasename']$_POST['nameofuser']$_POST['passofuser'] Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/#findComment-133063 Share on other sites More sharing options...
xProteuSx Posted November 30, 2006 Author Share Posted November 30, 2006 Thanks zanus! Now I got 'er up and running again ... Quote Link to comment https://forums.phpfreaks.com/topic/29031-multiple-variables-in-a-cookie/#findComment-133070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.