The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Store array in cookie
FMATeam
post Oct 5 2008, 03:03 PM
Post #1





Group: Members
Posts: 1
Joined: 5-October 08
Member No.: 6,820



I'm trying to store some array keys in a cookie
CODE

$USERS["user1"] = "random MD5 hash";
$USERS["user2"] = "another random MD5 hash";
$USERS["user3"] = "another random MD5 hash";

this is basically a login system and i'm trying to display the username used by any user when they login.
I'm trying to store the key in the cookie. Then recall it and print it using another file so it'll say "Welcome user1" or whatever.

Can anyone help...cheers
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Oct 5 2008, 11:09 PM
Post #2


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



I don't understand your (detailed) description of what you want to do, but to answer the subject Question...

(AFAIK) a cookie holds a string. So you need to "serialise" the array info (do you really mean the keys, user1 etc, or the hashes?) then save it.

If the values are (e.g.) alphanumeric strings, xxx, yyy, and zzz, then you can save "xxx:yyy:zzz" as the cookie value, using the implode() and explode() functions.

Alternatively you may be able to use the session functions.

Anyway the answer is in the php documentation somewhere...
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
SteveL
post Oct 13 2008, 04:09 AM
Post #3


Newbie
*

Group: Members
Posts: 19
Joined: 25-July 07
Member No.: 3,445



Your use case is *really* vague, but I'll take a shot at it. If you want to get at the keys in your $USERS array, its as simple as:

CODE

<?php
array_keys($USERS);  //This returns a scalar array with the keys of the input array as its values.
/*
So.. if your original array is:
$stuff = array("benny" => "jets", "piano" => "man");
The array that would come as the result of calling array_keys($stuff);
would look like array([0] => "benny", [1] => "piano");

*/

?>


My question is, what do you want to do with this array of keys? Do you want to store the entire array inside of a cookie? If so, you've got another step. If you want to store an individual cookie per user, your job is a lot easier...I think...again, without more details on how implement your original array, I'm not sure how to help...Going forward with the assumption that you want to store the entire array of keys in a cookie:

CODE

<?php
  $cookie_dough = serialize($stuff); // serialize returns a string that you can write in to your cookie...
// When you're ready to read your cookie and turn its contents back in to an array:
  $cookie = unserialize($string_i_read_from_cookie); //$cookie will be the array.
?>


Hope this helps!!

Cheers!

Steve

QUOTE(FMATeam @ Oct 5 2008, 03:03 PM) *

I'm trying to store some array keys in a cookie
CODE

$USERS["user1"] = "random MD5 hash";
$USERS["user2"] = "another random MD5 hash";
$USERS["user3"] = "another random MD5 hash";

this is basically a login system and i'm trying to display the username used by any user when they login.
I'm trying to store the key in the cookie. Then recall it and print it using another file so it'll say "Welcome user1" or whatever.

Can anyone help...cheers

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 01:10 PM