Welcome Guest, Not a member yet? Register   Sign In
Cookie not working in codeigniter 4. Need helper
#1

Hello I try to use the cookie system to store some information and also retrieve it I discovered that all the helper is not working.
In my library class fore remember me I try using the set_cookie(), get_cookie(), delete_cookie() after I have include the cookie helper through the function helper('cookie')
Reply
#2

Hi, can you post your code? Some guy on the forum was trying to set a cookie with a negative timestamp.
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#3

(04-19-2020, 02:12 PM)Leo Wrote: Hi, can you post your code? Some guy on the forum was trying to set a cookie with a negative timestamp.


Here is the my code for setting the cookie
if ($remember === 1) {
           
            set_cookie(array(
                'name' => getConfig('auth_cookie_id'),
                'value' => $this->_user->enc_key,
                'expire' => \time() + getConfig('auth_cookie_expiry'),
                'httponly' => FALSE
            ));

            // store encrypted key in cookie
            set_cookie(array(
                'name' => getConfig('auth_cookie_key'),
                'value' => $enkey,
                'expire' => \time() + getConfig('auth_cookie_expiry'),
                'httponly' => FALSE
            ));
        } else {
            // set what will be stored in the session
            $data = array(
                getConfig('auth_session_id') => $this->_user->enc_key,
                getConfig('auth_session_key') => $enkey
            );

            // store data in the session
            session()->set($data);
        }


Here, is where i try to get the cookie data
if (get_cookie(getConfig('auth_cookie_id'), TRUE) && get_cookie(getConfig('auth_cookie_key'), TRUE)) {
            // store cookie informations in variables
            $encryptedUserIDs = get_cookie(getConfig('auth_cookie_id'), TRUE);
            echo $enckey = get_cookie(getConfig('auth_cookie_key'), TRUE);

            $validate_using = 'cookie';
        } else {
            // store session informations in variables
            $encryptedUserIDs = session()->get(getConfig('auth_session_id'));
            $enckey = session()->get(getConfig('auth_session_key'));

            $validate_using = 'session';
        }
Reply
#4

(This post was last modified: 04-20-2020, 03:28 PM by Leo. Edit Reason: Clarity, switched array() with [] because we live in the 21st century )

I was able to set and read a cookie just fine with your stripped code. You probably have a problem with your getConfig() function. Heres a stripped to barebones code that worked. Just build your code up until you find the problem. Try and echo out getConfig('auth_cookie_expiry') and see if you get the correct value? Also your XSS_Clean set to true looks suspicious on the get_cookie() func. switch it to false maybe?

PHP Code:
    public function testSetCookies()
    {
        helper('cookie');
        set_cookie([
            'name' => 'auth_cookie_id',
            'value' => 'value_of_cookie',
            'expire' => time() + 1000,
            'httponly' => false
        
]);

        // store encrypted key in cookie
        set_cookie([
            'name' => 'auth_cookie_key',
            'value' => 'value_of_encrypted_cokie',
            'expire' => time() + 1000,
            'httponly' => false
        
]);
    }

    public function testReadCookies()
    {
        helper('cookie');
        if (get_cookie('auth_cookie_id'true) && get_cookie('auth_cookie_key'true)) {
            echo $_COOKIE['auth_cookie_id'];
            echo 
'<br>';
            echo $_COOKIE['auth_cookie_key'];
        }
    

Also try this extension in chrome. It really eases process of testing cookies.
http://www.editthiscookie.com/
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#5

(04-20-2020, 03:18 PM)Leo Wrote: I was able to set and read a cookie just fine with your stripped code. You probably have a problem with your getConfig() function. Heres a stripped to barebones code that worked. Just build your code up until you find the problem. Try and echo out getConfig('auth_cookie_expiry') and see if you get the correct value? Also your XSS_Clean set to true looks suspicious on the get_cookie() func. switch it to false maybe?

PHP Code:
    public function testSetCookies()
    {
        helper('cookie');
        set_cookie([
            'name' => 'auth_cookie_id',
            'value' => 'value_of_cookie',
            'expire' => time() + 1000,
            'httponly' => false
        
]);

        // store encrypted key in cookie
        set_cookie([
            'name' => 'auth_cookie_key',
            'value' => 'value_of_encrypted_cokie',
            'expire' => time() + 1000,
            'httponly' => false
        
]);
    }

    public function testReadCookies()
    {
        helper('cookie');
        if (get_cookie('auth_cookie_id'true) && get_cookie('auth_cookie_key'true)) {
            echo $_COOKIE['auth_cookie_id'];
            echo '<br>';
            echo $_COOKIE['auth_cookie_key'];
        }
    

Also try this extension in chrome. It really eases process of testing cookies.
http://www.editthiscookie.com/


I just used your code in my code but not working also
Reply
#6

I have been having problem with this cookie also yet still can not find way to use cookie in ci4 I am the guy he say I am.using wrong time
Hi leo I use you time stamp logic cokiee is not strong is goes away immediately set.
Reply
#7

(This post was last modified: 04-20-2020, 11:42 PM by Leo.)

"I just used your code in my code but not working also"

Oh, that's inconvenient. Try and set a cookie in a bare index.php page, without CI4, with PHP function

PHP Code:
<?php

$secure 
false;
if ((!empty(
$_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')) {
    $secure true;
}

setcookie("Cookie_name""Cookie_value"time() + 1000'/'''$secure); 



if still not working....well then off to the internet to look for reasons why cookie not setting  Confused

P.S. also try in different browsers.
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply
#8

Cookie working fine with ci3 on the same browser i am trying to set cookie on in ci4 We actually can not find a clear sollution what is actually wrong with cookie here i was tracking ip insted of use cookie when i try for so many day and i was stuck in cookie i need to use another logic instead.
Reply
#9

(This post was last modified: 04-21-2020, 05:58 AM by Leo.)

Weird. Is your CI4 composer installed or unzipped?

Try and add this 'domain' => ''
and maybe this 'secure' => false,

then test with this:

PHP Code:
public function testSetCookies()
    {
        helper('cookie');
        set_cookie([
            'name' => 'auth_cookie_id',
            'value' => 'value_of_cookie',
            'expire' => time() + 1000,
            'domain' => '',
            
'secure' => false,
            'httponly' => false
        
]);

        // store encrypted key in cookie
        set_cookie([
            'name' => 'auth_cookie_key',
            'value' => 'value_of_encrypted_cokie',
            'expire' => time() + 1000,
            'domain' => '',
            
'secure' => false,
            'httponly' => false
        
]);
    }

    public function testReadCookies()
    {
        helper('cookie');
        if (get_cookie('auth_cookie_id'true) && get_cookie('auth_cookie_key'true)) {
            echo $_COOKIE['auth_cookie_id'];
            echo '<br>';
            echo $_COOKIE['auth_cookie_key'];
        }
    

I just noticed that if I set any type of domain in cookie set up they wouldn't work. Please write back if that did it.

Also check what you have set up in App/Config
I have this:
PHP Code:
    public $cookiePrefix   '';
    public 
$cookieDomain   '';
    public 
$cookiePath     '/';
    public 
$cookieSecure   false;
    public 
$cookieHTTPOnly false
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB