Help Understanding Cookies

#1
[eluser]sico87[/eluser]
I was hoping someone, could give some information on Cookies and Codeigniter, I have read there user guide on them so I have an understanding of how to set them, etc. What I want to know is how do I use for example if you look at the BBC website, you can drag and drop the boxes and it will remember where you placed them with out the need for you to sign up or login in etc, so I assume it is being stored in the the cookie.

On my site, I have a menu system that allows the user to choose the content that features on 'their' page, now I don't want them to have to sign up or log in so I assume I could somehow store the content they requested in the cookie? Is this possible? Currently they click a link and the relevant content is pulled from the database using the ID in the URL and the ID in the database a match. Do I just save each ID that is requested? And if so how?

#2
[eluser]Sbioko[/eluser]
Sure, you can use cookies for your task. You can set them with native php setcookie function or using cookie helper.

#3
[eluser]ShiverCube[/eluser]
You could use the cookie helper set_cookie() and get_cookie() functions. Example:

Code:
$this->load->helper('cookie');
set_cookie(array(
   'name'   => 'settings',
   'value'  => serialize($settings),
   'expire' => 86500,
   'domain' => '.mydomain.com',
   'path'   => '/'
));

// Load the settings
$settings = get_cookie('settings', TRUE);
if ($settings !== FALSE)
{
    $settings = unserialize($settings);
}
else
{
    // Use default settings
}

But an even easier option would be to use the Session Library

#4
[eluser]Petsoukos[/eluser]
I can't set any cookies with CI's set_cookie.. only with native PHP setcookie()

Does it have to do with the domain? I'm on a local machine and the domain is localhost. The CI has its own folder http://localhost/folder?

#5
[eluser]WanWizard[/eluser]
'localhost' is an invalid cookie domain name, according to the RFC. Modern browsers will refuse the cookie.

Do not use localhost, set up a test domain locally (p.e. site.mylaptop.local) , add the entry in your hosts file, and use it in your vhost or webserver configuration.

In the case of PHP's setcookie, the manual states
Quote:session.cookie_domain specifies the domain to set in the session cookie. Default is none at all meaning the host name of the server which generated the cookie according to cookies specification.

See http://stackoverflow.com/questions/11342...cit-domain

#6
[eluser]Petsoukos[/eluser]
I guessed that it would be the domain.. I'll configure the HOSTS and test it out...

Thanks!

#7
[eluser]Petsoukos[/eluser]
Now I can't set a cookie with expiration with PHP's native function setcookie()... I can set it if I provide the name and value of the cookie and expiration 0 but as soon as I provide the expiration (example: 86400) it wont set the cookie... what am I doing wrong ?

#8
[eluser]WanWizard[/eluser]
setcookie() requires an (absolute) expiry time, not a time interval. So use time() + 86400, not just 86400.

#9
[eluser]Petsoukos[/eluser]
Yeah I missed that...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.