Welcome Guest, Not a member yet? Register   Sign In
cookies and CI4
#1

Hi guys, 

I am having a really hard time setting a cookie in CI4, And i have no idea how to diagnose this.

I have followed the information https://codeigniter.com/user_guide/helpe...elper.html and wrote a set cookie.

$expires  = date('Y-m-d H:iConfused', time() + 1000);
set_cookie([
    'name' => 'auth_cookie_id',
    'value' => 'value_of_cookie',
    'expire' => $expires,
    'httponly' => false
]);


My problem is this cookie never generates. Im using localhost and its either the php environment (in windows) or the default config settings for cookies (or the .env file). As there is no information on this for CI4 and I have never worked with cookies before I have little knowledge to draw from to try and resolve this problem.

I have googled loads, and found tones of information, none of which seemed to help generate a cookie for me.

What i would like to ask is if someone could provide a working example using localhost for CI4 so i can look at it and see whats different for me.

How you set the cookie (I assume in the controller), how it gets to the user (returning back from contropller sends the cookie on $response??)
How to check the cookie got to the user - Currently im using google chrome pressing F12 and checking the Cookies section (i can see the ci_session cookie and the debugbar cookies fine)

Is there anything i would need to enable in the php.ini file for this too? I have not used XAMMP, but pure php install and modifying the ini as I go for the bits i need (turning on curl, email support etc)

Sorry to ask this here, Its just i simply cannot find any good resource for this problem.
Reply
#2

(This post was last modified: 07-14-2020, 10:53 AM by kristianlake.)

Okay so i have made some progress.

helper('cookie');

            setcookie('user2', 'value2', time() + 6000, '/', '');   <--- works!

            set_cookie([
                'name' => 'auth_cookie_id',
                'value' => 'value_of_cookie',
                'expire' => time() + 6000,
                'path' => '/',
                'domain' => ''
            ]);

The first setcookie actually works.

I will keep going on this, but hoping someone can help me out Smile

Finally figured it out.

in my controller i was calling

public function myFunction()
{
//works but is PHP
setcookie('user2', 'value2', time() + 6000, '/', '');

//this did not work, never returned anything back.
set_cookie([
'name' => 'user3',
'value' => 'value3',
'expire' => time() + 6000
]);

//this worked, not sure if this is the best way though
return $this->myFunctionOK($user)->setCookie([
'name' => 'user4',
'value' => 'value4',
'expire' => time() + 6000
]);
}

So my real question is on user3, did i need to do something to make it send back? or did i need to return it as part of myFunctionOK.

Attached Files Thumbnail(s)
   
Reply
#3

Haven't looked at the code for set_cookie, so can't tell why that's not working.
But for setting the cookie with setCookie() you can do like this, so you can set multiple.

PHP Code:
public function myFunction()
{
    
$this->request->setCookie([
        
'name' => 'user4',
        
'value' => 'value4',
        
'expire' => time() + 6000
    
]);
    
    return 
$this->myFunctionOK($user);

Reply
#4

(This post was last modified: 07-14-2020, 01:06 PM by kristianlake.)

(07-14-2020, 12:12 PM)jreklund Wrote: Haven't looked at the code for set_cookie, so can't tell why that's not working.
But for setting the cookie with setCookie() you can do like this, so you can set multiple.

PHP Code:
public function myFunction()
{
    
$this->request->setCookie([
        
'name' => 'user4',
        
'value' => 'value4',
        
'expire' => time() + 6000
    
]);
    
    return 
$this->myFunctionOK($user);


Thanks. under set_cookie it does the request->setCookie functionality, it just ensures it adheres to the config settings being assigned and ensures that request is not null.

The fact your setting it into $this->request->setCookie almost implies i need to assign set_cookie to the controller. I thought that was the point of the cookie helper class though, so thats why Im confused over it still Smile
Reply
#5

My God! I had the same problem. I used set_cookie() in version 4, and it did nothing. I loaded the helper() a thousand times, and never got it to work until I came across this post! I read this post several times until I saw setcookie(). The confusion is that these two functions are so similar, and I did not see it right away. Of course, that does not explain why set_cookie() does not work.
Reply
#6

It works fine.

PHP Code:
<?php

namespace App\Controllers;

class 
Home extends BaseController
{
    public function index()
    {
        helper('cookie');
        set_cookie([
            'name'  => 'auth_cookie_id',
            'value'  => 'value_of_cookie',
            'expire' => time() + 6000,
        ]);
    }


[Image: v0ekpT.png]
Reply
#7

Hi!

`setcookie` is the native PHP function. `set_cookie` is CI4's cookie helper function.

Behind the scenes, when calling `set_cookie`, you are actually building a cookie object to be saved in the Response class for dispatching later. Please note that cookie objects created by `set_cookie` are just sitting there in the Response class. They are not yet dispatched to the browser. That is why you are complaining the `set_cookie` does not work. It works its job but not the way you expect it to be.

When you dispatch the cookies via the Response class, the Response calls CookieStore to actually do the dispatching (this will be changed in future versions where Response will do the dispatching). When the cookies are dispatched, under the hood it calls the native `setcookie` function which actually sets the cookies into the browser. This way the cookies are available for retrieval by `get_cookie` or thru access from the `$_COOKIE` array.

In CI4, to make `set_cookie` actually work in your case, you need to have the saving and retrieval of cookies in two different places in your controller. The `set_cookie` code in one and the `get_cookie` in another.

PHP Code:
public function one()
{
    
set_cookie(...);

    return 
redirect()->to('two')->withCookies();
}

public function 
two()
{
    
get_cookie(...);

Reply
#8

Also keep in mind that cookies do not show up in a view until the next page refresh.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

Yeah, cookie is a bit confusing.

Code:
Browser <-- Response --- CI4
Browser --- Request ---> CI4

Code:
set_cookie()    <-- Response
delete_cookie() <-- Response
has_cookie()    <-- Response
get_cookie()    <-- Request


https://www.codeigniter.com/user_guide/h...elper.html
Reply




Theme © iAndrew 2016 - Forum software by © MyBB