Help Understanding Cookies

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?

Sure, you can use cookies for your task. You can set them with native php setcookie function or using cookie helper.

You could use the cookie helper set_cookie() and get_cookie() functions. Example:

   'name'   => 'settings',
   'value'  => serialize($settings),
   'expire' => 86500,
   'domain' => '',
   'path'   => '/'

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

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

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?

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


I guessed that it would be the domain.. I'll configure the HOSTS and test it out...


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 ?

setcookie() requires an (absolute) expiry time, not a time interval. So use time() + 86400, not just 86400.

Yeah I missed that...

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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