Udders
Udders

Reputation: 6986

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'));`

Upvotes: 5

Views: 14581

Answers (4)

Steve Parish
Steve Parish

Reputation: 1874

Anyone else finding odd results when using codeignighters cookies be sure that the cookie was set using codeignighters $this->input->set_cookie instead of PHP's own setcookie method.

I had strange results until I clear all cookies and reset it with codeignighters own method.

Upvotes: 1

shanavas
shanavas

Reputation: 1

fist of all you need to know the basic Idea about cookie Session is creating in server where as a cookie is creating in the clients browser.

so the cookie get created when ever you browse the php file (In which the set cookie code is ter)

And from the next request onward you will get the section to read

Upvotes: 0

Corey Ballou
Corey Ballou

Reputation: 43547

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);
}

Upvotes: 14

Jan Hančič
Jan Hančič

Reputation: 53929

Use the Cookie Helper to get/set cookies.

Upvotes: 2

Related Questions