Codeigniter Cookies Help


Question

Hello in codeigniter how would I check if the user is visiting the site for the first time, and if they are set a cookie?

I am already using the Session library and database sessions which stores the session_id etc, but I need to to be able to check if the user is a first time visitor and if they have a cookie already `

$cookie = array(
                           'name'   => 'some_value',
                           'value'  => 'The Value',
                           'expire' => time()+86500,
                           'domain' => '.some-domain.com',
                           'path'   => '/',
                           'prefix' => '',
                       );

        set_cookie($cookie);
        var_dump(get_cookie('some_value'));`
1
5
1/13/2010 10:54:24 PM

Accepted Answer

Using the cookie helper, you could check if a user is a first time visitor by doing:

if (!get_cookie('some_value')) {
    // cookie not set, first visit

    // create cookie to avoid hitting this case again
    $cookie = array(
        'name'   => 'some_value',
        'value'  => 'The Value',
        'expire' => time()+86500,
        'domain' => '.some-domain.com',
        'path'   => '/',
        'prefix' => '',
    );
    set_cookie($cookie);
}
14
1/13/2010 11:59:27 PM

Use the Cookie Helper to get/set cookies.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon