Cookie not working in codeigniter 4. Need helper |
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')
(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'; }
04-20-2020, 03:18 PM
(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() 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.
(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? I just used your code in my code but not working also
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.
"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 if still not working....well then off to the internet to look for reasons why cookie not setting 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.
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.
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() 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 = '';
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
|
Welcome Guest, Not a member yet? Register Sign In |