Gossamer Forum
Home : General : Internet Technologies :

Cookie Array Question

Quote Reply
Cookie Array Question
I have set my cookies as an array so that all the info is stored in 1 cookie.

Code:


<?php setcookie("ptdetails[user]", "$user", time()+60*60*24*30, "/");
setcookie("ptdetails[pword]", "$pword", time()+60*60*24*30, "/");
setcookie("ptdetails[usertype]", "$usertype", time()+60*60*24*30, "/");
setcookie("ptdetails[secure]", "$secure", time()+60*60*24*30, "/");
setcookie("ptdetails[pagestyle]", "$pagestyle", time()+60*60*24*30, "/");

header('Location: index.php') ?>


Now I am trying to get the information out of the cookies and so far having no luck.

Code:


if(!isset($user) OR !isset($pword) OR !isset($usertype) OR !isset($secure) OR !isset($pagestyle)) {

print_r($_COOKIE);

echo("<hr />");

foreach ($_COOKIE['ptdetails'] as $name => $value) {
echo "$name : $value <br />\n";

$value = $ptinfo[$name];;

}

echo("<hr />");

print_r($ptinfo);


}


I've looked through the php documentation on both cookies and arrays but I cannot find the answer to the problem I'm having. Anyone's help would be appreciated.


By the way. When I tested it using the print_r($_COOKIE); and the echo "$name : $value <br />\n"; I got the following:

Quote:


Array ( [ptdetails] => Array ( [user] => Josh [pword] => something [usertype] => admin [secure] => true [pagestyle] => default ) )


user : Josh
pword : something
usertype : admin
secure : true
pagestyle : default

Last edited by:

JoFrRi: Nov 4, 2003, 9:44 AM
Quote Reply
Re: [JoFrRi] Cookie Array Question In reply to
Why not just use normal names? i.e;

setcookie(USER, "$user", time()+60*60*24*30, "/");
setcookie(PWORD, "$user", time()+60*60*24*30, "/");

...etc.

Then just call it with;

$_COOKIE[USER]

Unsure

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Cookie Array Question In reply to
Because I wanted the cookies to be stored in 1 array. It's all good though, I got it to work, it was actually quite simple. To get the array from within the array I just had to do the following:

$ptdetails = $_COOKIE[ptdetails];

$user = $ptdetails[user];
$pword = $ptdetails[pword];
$usertype = $ptdetails[usertype];
$secure = $ptdetails[secure];
$pagestyle = $ptdetails[pagestyle];

And yes, this took me a few hours to figure out with guess and check :P

But I've got another question now... I have read some documentation for the count_chars function. When I try this function, I get an array as my result instead of just the result. So I followed an example that used mb_strlen() instead. When I did this I got the following error:

Fatal error: Call to undefined function: mb_strlen() in c:\apache\htdocs\_other\presenttruth_new\test.php on line 4


If anyone could explain why I'm getting an array or why I'm getting this error, it'd be helpful.
Quote Reply
Re: [JoFrRi] Cookie Array Question In reply to
No prob. Regarding your other question, have you considered strlen() ?

Code:
<?php
$str = 'abcdef';
echo strlen($str); // 6

$str = ' ab cd ';
echo strlen($str); // 7
?>

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Cookie Array Question In reply to
Wonderful, wonderful, it works wonderfully! :D Thanks for your help. Probably not the first time.