SyncViews Posted December 17, 2007 Share Posted December 17, 2007 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']; Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2007 Share Posted December 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 17, 2007 Share Posted December 17, 2007 Or see example #3 in the manual - http://php.net/setcookie Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 17, 2007 Author Share Posted December 17, 2007 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"; } Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted December 17, 2007 Share Posted December 17, 2007 I'd say you'll be better of serializing your cookie array. That way you can just setup an array of what you want your cookie to contain. Rather than having to setup a cookie for every item of your array. Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 17, 2007 Author Share Posted December 17, 2007 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted December 17, 2007 Share Posted December 17, 2007 Works fine here. Try clearing the syncproductions cookie from your browser and retest your code. I dont think PHP automatically unserializes cookies. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2007 Share Posted December 17, 2007 You may need to use the stripslashes() function before you unserialize. Dump the contests of the $_COOKIE array to see what's there: <?php echo '<pre>' . print_r($_COOKIE,true) . '</pre>'; ?> Ken Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 17, 2007 Author Share Posted December 17, 2007 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> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 17, 2007 Share Posted December 17, 2007 See my previous post ... you need to use the stripslashes() function before you unserialize: <?php $cookie = unserialize(stripslashes($_COOKIE['syncproductions'])); ?> Ken Quote Link to comment Share on other sites More sharing options...
SyncViews Posted December 17, 2007 Author Share Posted December 17, 2007 $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 Quote Link to comment 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.