Jump to content

[SOLVED] setting array in cookie


SyncViews

Recommended Posts

This doesn't seem to work as I get an error on the setcookie function :(

How should I store the values as a cookie without having to create 5 seprate cookies?

<?php
$domain = 'syncproductions.exofire.net';

$cookie = array
(
	'ID'         => $user_id,
	'UserName'   => $user_name,
	'Password'   => $user_pass,
	'Persistant' => '1'
);

setcookie('syncproductions', $cookie, time() + 60*60*24*100, '/','.' . $domain);
?>

Then read with

$login_user = $_COOKIE['syncproductions']['UserName'];
$login_pass = $_COOKIE['syncproductions']['Password'];

Link to comment
https://forums.phpfreaks.com/topic/82045-solved-setting-array-in-cookie/
Share on other sites

That's because the second argument needs to be a string, you're passing an array. You need to serialize the array when setting the cookie and unserialize it when retrieving the information:

<?php
$domain = 'syncproductions.exofire.net';
$cookie = array(
          'ID'         => $user_id,
          'UserName'   => $user_name,
          'Password'   => $user_pass,
          'Persistant' => '1');
setcookie('syncproductions', serialize($cookie), time() + 60*60*24*100, '/','.' . $domain);
?>

 

Ken

ok I used the method shown in the 3rd example but npow how do I get certain values?

 

I tried this but it doesn't get the value out :(

$_COOKIE['syncproductions']['UserName']

 

I suppose I could do this but i'm sure theres a better way...

 

foreach ($_COOKIE['syncproductions'] as $name => $value)

{

if ($name == 'UserName') echo "$name : $value <br />\n";

}

 

Whats wrong with this? (cause the value of the cookie is a string right?)

"Warning: unserialize() expects parameter 1 to be string, array given in /backup/home/syncview/public_html/new/test.php on line 22"

<?php
if ($_GET['page'] == 1)
{
	$user_id = 1;
	$user_name = 'Sync Views';
	$user_pass = 'password';

	$domain = 'syncproductions.exofire.net';

	$cookie = array
	(
		'ID'         => $user_id,
		'UserName'   => $user_name,
		'Password'   => $user_pass,
		'Persistant' => '1'
	);

	setcookie('syncproductions', serialize($cookie), time() + 60*60*24*100, '/','.' . $domain);
}
elseif ($_GET['page'] == 2)
{
	$cookie = unserialize($_COOKIE['syncproductions']);
	echo $cookie['ID'] . '<br>';
	echo $cookie['UserName'] . '<br>';
	echo $cookie['Password'] . '<br>';
	echo $cookie['Persistant'] . '<br>';
}
?>

Well i deleted the cookie from my browser but it still doesn't read the cookie :(

 

<?php
if ($_GET['page'] == 1)
{
	$user_id = 1;
	$user_name = 'Sync Views';
	$user_pass = 'password';

	$domain = 'syncproductions.exofire.net';

	$cookie = array
	(
		'ID'         => $user_id,
		'UserName'   => $user_name,
		'Password'   => $user_pass,
		'Persistant' => '1'
	);

	setcookie('syncproductions', serialize($cookie), time() + 60*60*24*100, '/','.' . $domain);
}
elseif ($_GET['page'] == 2)
{
	$cookie = unserialize($_COOKIE['syncproductions']);
	echo $_COOKIE['syncproductions'] . "<br> \n";
	echo $cookie['ID'] . "<br> \n";
	echo $cookie['UserName'] . "<br> \n";
	echo $cookie['Password'] . "<br> \n";
	echo $cookie['Persistant'] . "<br> \n";;
}
?>

On the 2nd page it just shows:

a:4:{s:2:\"ID\";i:1;s:8:\"UserName\";s:10:\"Sync Views\";s:8:\"Password\";s:8:\"password\";s:10:\"Persistant\";s:1:\"1\";}<br> 
<br> 
<br> 
<br> 
<br> 

$cookie = serialize(stripslashes($_COOKIE['syncproductions']));
echo $_COOKIE['syncproductions'] . "<br> \n";
echo $cookie['ID'] . "<br> \n";
echo $cookie['UserName'] . "<br> \n";
echo $cookie['Password'] . "<br> \n";
echo $cookie['Persistant'] . "<br> \n";
echo '<pre>' . print_r($_COOKIE,true) . '</pre>';

a:4:{s:2:\"ID\";i:1;s:8:\"UserName\";s:10:\"Sync Views\";s:8:\"Password\";s:8:\"password\";s:10:\"Persistant\";s:1:\"1\";}
s
s
s
s

Array
(
    [phpSESSID] => 49cdb37ff5ab2179b158f06cdd35350c
    [syncproductions] => a:4:{s:2:\"ID\";i:1;s:8:\"UserName\";s:10:\"Sync Views\";s:8:\"Password\";s:8:\"password\";s:10:\"Persistant\";s:1:\"1\";}
)

 

EDIT: ops..typo lol... works now :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.