CodeIgniter Forums
troubles with cookies - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: troubles with cookies (/showthread.php?tid=2357)



troubles with cookies - El Forum - 07-31-2007

[eluser]andycorpes[/eluser]
Hi Group

I'm new to CI, and relatively new to PHP, however, using raw php i CAN get cookies to work.

Using CI, i can set cookies, but cannot seem to retrieve them afterwards. Are there any "gotchas" using the CI cookie helper? or am i just being dumb after a long day? I've tried various "lifetimes", from -1 to delete the rubbish i created, through to 315360000 (10 years?)

Originally, i coded this using the session class, which all worked, however, i needed one set of session variables to live for 24 hours, and the other set to live forever (Hence 10 years). I encountered problems with the session class where ALL session variables got the same lifetime, i.e i wrote the 24 hour session variables, and it changed the life time of my 10 year session variables.

If anyone understands what i am trying to do, and knows of an alternative approach, i'd be happy to try it.

Hopefully, fresh eyes will solve my problems tomorrow.

Andy Corpes


troubles with cookies - El Forum - 07-31-2007

[eluser]Michael Wales[/eluser]
Could you share some of your code so we can help you get it working. The Cookie Helper documentation is pretty self-explanatory, so I think it may be an error somewhere.


troubles with cookies - El Forum - 08-01-2007

[eluser]andycorpes[/eluser]
Hi Micheal

here is the code i use to write (&merge;with anything already present)

$wlocationdata = get_cookie($pindex);
if (is_string($wlocationdata)) {
$wlocationdata = unserialize($wlocationdata);
} else {
$wlocationdata = array();
}

$wnewlocationdata[$plocation]['name'] = $plocation;
$wnewlocationdata[$plocation]['desc'] = $pdesc;
$wnewlocationdata[$plocation]['files'] = $pfiles;

$wmergedlocationdata = array_merge($wlocationdata,$wnewlocationdata);

$wserialdata = serialize($wmergedlocationdata);
echo "\nadding cookie:$pindex data:$wserialdata\n";

set_cookie($pindex,$wserialdata,$this->tenyears);

and i use this to read:-

$wlocationdata = get_cookie($pindex);

if (is_string($wlocationdata)) {
$wnewlocationdata = unserialize($wlocationdata);
} else {
$wnewlocationdata = array();
}

echo "\nreading cookie:$pindex data:$wlocationdata\n";


The echo's show:-
adding cookie:loc03 data:a:1:{s:6:"test01";a:3:{s:4:"name";s:6:"test01";s:4:"desc";s:6:"desc01";s:5:"files";a:1:{i:0;i:0;}}}

reading cookie:loc03 data:

it's driving me nuts !!


troubles with cookies - El Forum - 08-01-2007

[eluser]Michael Wales[/eluser]
Wrapping it in code tags so I can view it:

Code:
<?php
$wlocationdata = get_cookie($pindex);
if (is_string($wlocationdata)) {
  $wlocationdata = unserialize($wlocationdata);
} else {
  $wlocationdata = array();
}

$wnewlocationdata[$plocation][’name’] = $plocation;
$wnewlocationdata[$plocation][’desc’] = $pdesc;
$wnewlocationdata[$plocation][’files’] = $pfiles;

$wmergedlocationdata = array_merge($wlocationdata,$wnewlocationdata);

$wserialdata = serialize($wmergedlocationdata);
echo “\nadding cookie:$pindex data:$wserialdata\n”;

set_cookie($pindex,$wserialdata,$this->tenyears);
?>

Code:
<?php
$wlocationdata = get_cookie($pindex);

if (is_string($wlocationdata)) {
  $wnewlocationdata = unserialize($wlocationdata);
} else {
  $wnewlocationdata = array();
}

echo “\nreading cookie:$pindex data:$wlocationdata\n”;
?>



troubles with cookies - El Forum - 08-01-2007

[eluser]andycorpes[/eluser]
Hi Group,

I've also just tried the code below, without any success:-


set_cookie("testcookie","testdata",36000);

$wtestdata = get_cookie("testcookie");

echo "\ntestdata:$wtestdata\n";

$wtestdata is empty,, is this my config perhaps?


troubles with cookies - El Forum - 08-01-2007

[eluser]andycorpes[/eluser]
Sorry about the lack of code tags,, is this better?

Code:
set_cookie("testcookie","testdata",36000);
      
       $wtestdata = get_cookie("testcookie");
      
       echo "\ntestdata:$wtestdata\n";



troubles with cookies - El Forum - 08-01-2007

[eluser]andycorpes[/eluser]
Hi again

I've now deleted all the cookies from my machine and rerun the code,, same result.

So i then moved the code into the view file (from the model), and tried again,

this time, it wrote the cookie.

As the model code runs before the headers are sent, i guess the cookie writing has to be elsewhere, i suspect that it must go in the view code, or i can go into the controller, after the view has processed.

Can anyone confirm my suspicions?

Andy


troubles with cookies - El Forum - 08-01-2007

[eluser]Fabian Wesner[/eluser]
You must set the cookie bevor any echo output.

This is the wrong:
Code:
echo “\nadding cookie:$pindex data:$wserialdata\n”;
set_cookie($pindex,$wserialdata,$this->tenyears);



troubles with cookies - El Forum - 08-02-2007

[eluser]andycorpes[/eluser]
Hi group

My problem is now resolved. I suspect it was caused by my liberal use of the echo statement, as stripping them out to make the code presentable for this forum seemed to do the trick.

Thanks to everyone who replied.

Andy