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
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

Link to comment
Share on other sites

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";

}

 

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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> 

Link to comment
Share on other sites

$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 :)

Link to comment
Share on other sites

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.