Welcome Guest, Not a member yet? Register   Sign In
Problems with cookies
#1

[eluser]Dani[/eluser]
Hi, I have some trouble with saving cookies.
I am trying to save form data into cookies. For this I have the following in my controller where the form is returned to:

Code:
print_r($this->input->post());

foreach($this->input->post() as $key=>$value)
{
  $this->input->set_cookie($key,$value);

}


echo "<hr>cookies: <br>";
print_r($_COOKIE);


Output of this is:
Code:
Array ( [currency] => USD [unit_size] => 40 [adress] => some street [submit] => Save )

cookies:
Array ( )


- Google Chrome debugger -> Resources -> cookies -> localhost just displays "This site has no cookies".
- I have tried switching to 127.0.0.1 both in the url and in the config.php.
- I have tried setting up dev.livesite.com and setting cookie domain to .livesite.com in config.php as described here http://stackoverflow.com/questions/39021...-localhost

Any suggestions?
#2

[eluser]Aken[/eluser]
Are you using sessions already? If so, why not save it as session userdata instead?
#3

[eluser]Dani[/eluser]
I don't use sessions, not explicit at least. The reason I prefer cookies is that I want to save user settings, and session expires as soon as you close your browser. I could ofc save settings to db, but that would require more coding and load time as I then would have to set up a multiuser system (user table, etc).
#4

[eluser]tonyurso[/eluser]
Usually you wont see cookies on the same page that you set them.
#5

[eluser]Dani[/eluser]
[quote author="tonyurso" date="1354657636"]Usually you wont see cookies on the same page that you set them.[/quote]So then the cookies should be set when navigating to other pages on the site? They are not.
#6

[eluser]CroNiX[/eluser]
You can't set a cookie and immediately read it without a refresh. The cookie doesn't get written until the final output is sent to the browser since it's transmitted in the headers of the request...
#7

[eluser]Dani[/eluser]
I have tried to navigate to other pages and refresh and refresh. Still no cookies are set.
#8

[eluser]CroNiX[/eluser]
Post all cookie values from your config settings.

As a side, it would be much better to serialize (or json encode) your post array one time and store that in a single cookie rather than an individual cookie for each post value. Then unserialize it when reading it back.
#9

[eluser]Dani[/eluser]
config.php (@localhost)
Code:
$config['sess_cookie_name']  = 'ci_session';
$config['sess_expiration']  = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name']  = 'ci_sessions';
$config['sess_match_ip']  = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;

/*
|--------------------------------------------------------------------------
| Cookie Related Variables
|--------------------------------------------------------------------------
|
| 'cookie_prefix' = Set a prefix if you need to avoid collisions
| 'cookie_domain' = Set to .your-domain.com for site-wide cookies
| 'cookie_path'   =  Typically will be a forward slash
| 'cookie_secure' =  Cookies will only be set if a secure HTTPS connection exists.
|
*/
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path']  = "/";
$config['cookie_secure'] = FALSE;


Ok, I'll look into serializing.




Theme © iAndrew 2016 - Forum software by © MyBB