Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter Cookies Help

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'));`
like image 839
Udders Avatar asked Jan 13 '10 22:01

Udders


People also ask

Where are Cookies stored in Codeigniter?

Cookies Codeigniter are small data files that store specific information about the sessions. This could login status and cart contents. These files are stored in a “Cookies” folder (the name could differ).

What is CI session cookie?

Sessions in Codeigniter with examples One more thing is cookies store information in the end-user browser until the user deletes it or its time doesn't get over. The session is a server-side data stored on a temporary basis. As the user closes the browsers, the session will automatically get removed.


2 Answers

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);
}
like image 171
Corey Ballou Avatar answered Oct 04 '22 06:10

Corey Ballou


Use the Cookie Helper to get/set cookies.

like image 31
Jan Hančič Avatar answered Oct 04 '22 07:10

Jan Hančič