19 January 2009 ~ Comments Off

CodeIgniter – Write a cookie

This will show you how easy it is to set a cookie using CI (CodeIgniter) the PHP framework.


First we need to include the helper into your controller. The other option is to add this to the autoload.php file in your config folder.

1
$this->load->helper('cookie');

Now we need to set the cookie

1
2
3
4
5
6
7
8
9
10
$cookie = array(
                   'name'   => 'The Cookie Name',
                   'value'  => 'The Value',
                   'expire' => '86500',
                   'domain' => '.some-domain.com',
                   'path'   => '/',
                   'prefix' => 'myprefix_',
               );
 
set_cookie($cookie);

OR

1
set_cookie($name, $value, $expire, $domain, $path, $prefix);

Now that we have set the cookie lets get it!

1
get_cookie('some_cookie');

Done, now you know how to set a cookie in CI!

Tags: ,

Comments are closed.