Codeignitier "set_cookie" is not working - php

I'm trying to set a cookie in CodeIgnitier with no luck.
I was searching for a solution, and i found some posts also here on Stackoverflow, but none of them actually solved the problem for me.
The code is:
$this->load->helper('cookie');
set_cookie('username',$this->input->post('username'));
No cookie was set after execution.
In order to avoid wrong answers:
'secure' is set to FALSE in config file, everything else is also as default.
According to the browser, no cookie was set. only Codeignitier native cookie - ci_session is listed there.

Set it via array like so:
$cookie = array(
'name' => 'username',
'value' => $this->input->post('username'),
'expire' => '0', // expiration time 0 is until browser closes, set to large number for 'remember me' cookie
'domain' => '.mysite.com', // set this to the domain the cookie will be under with a leading .
'path' => '/',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($cookie);

Related

Setting a cookie in codeigniter 4 is not working

I'm currently working on a project where I have to migrate it from codeigniter version 3 to version 4. When setting a cookie, the following code was originally used:
$cookie = array(
'name' => 'admin_id',
'value' => $result['id'],
'expire' => '216250',
'domain' => '',
'path' => '/',
'prefix' => '',
'secure' => FALSE
);
$this->input->set_cookie($cookie);
According to the docs, the way to set a cookie in version 4 is by the following code:
set_cookie($cookie);
Note that the $cookie variable is still the same.
This cookie is being set in the Model file and then a redirect is called in the following way:
return redirect()->to('/');
I'm aware that cookies are only set after a page is being redirected and therefore I'm trying to retrieve it in another controller. However, the cookie is not being set.
use withCookies()
return redirect()->to('/')->withCookies();
see https://github.com/codeigniter4/CodeIgniter4/issues/3939

Yii2 Session persistance when redirecting from one action to another

Using Yii2 framework:
The code below creates an endless loop.
Can anyone please explain how I make the session data persist on redirect ?
I have checked and there is not data being transferred, but the session data is set inside searchuser correctly.
public function actionSearchUser()
{
$session = \Yii::$app->session;
$session->open();
$session->set('admin.currentuser.id', "This worked out ok");
return $this->redirect(['site/modify-user']);
}
public function actionModifyUser()
{
$session = \Yii::$app->session;
$session->open();
if( !($session->has('admin.currentuser.id')) )
{
return $this->redirect(['site/search-user']);
}
else return $this->render('modifyUser');
}
And here is where I setup my session:
'session'=>array(
'class' => 'yii\web\Session',
'name' => 'SESSIONNAME',
'timeout' => 86400,
'savePath' => '/path/to/sessions',
'useCookies' => true,
'cookieParams' => array(
'lifetime' => 86400,
'path' => '/',
'domain' => 'localhost',
),
),
My problem was the domain (I know, I'm stupid).
I have a custom domain (n099y.local) so I needed to change the cookie domain from localhost to n099y.local and everything was fine.
It was showing all the correct session data on the page until I went to another page when the data was again missing because the cookie domain did not match the domain I was on.

Cookies are there, but i can't get them in CodeIgniter. Why?

Codeigniter: When user logs in I want to save a cookie containing email.
$cookie = array(
'name' => 'email',
'value' => $email,
'expire' => time()+3600*24*30*30,
//'domain' => '.racebooking.net',
'path' => '/',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
Once this code runs, f i look in Firebug i can see the cookie has been properly set. Here is what i see:
name value domain raw size path expires Security
email fontanavideostudios#gmail.com www.test.racebooking.net 36 B / 07/09/2062 20:50:15 Secure
Unfortunately, when i try to retrieve it as follows
$this->input->cookie('email', TRUE);
I get nothing at all. The cookie is there, but i can't get it in CI. Any idea why? The website is installed in www.test.racebooking.net (a subdomain i use for testing before going on production)
Ok, (I think) I found the problem! By setting .racebooking.net as domain, instead of leaving it blank (and, hence, automatically setting as www.test.racebooking.net) i fixed it.
$cookie = array(
'name' => 'email',
'value' => $email,
'expire' => time()+3600*24*30*30,
'domain' => '.racebooking.net',
'path' => '/',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
Now i can retrieve cookie values.
The "why does this happen" question remains still open.
cookie('some_cookie');
The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist.
Source: https://www.codeigniter.com/user_guide/libraries/input.html
There is also:
https://www.codeigniter.com/user_guide/helpers/cookie_helper.html

How to delete COOKIE in YII which is set in codeigniter

i//Setting cookie in codeigniter
In codeigniter:
$this->load->helper('cookie');
$cookie = array(
'name' => 'social',
'value' => 'logout',
'expire' => 86500,
'secure' => false
);
$this->input->set_cookie($cookie);
In YII:
if(isset($_COOKIE['social'])&&$_COOKIE['social']=='logout'){
//Clearing cookie in yii
Yii::app()->request->cookies->clear();
Yii::app()->user->logout();
}
A pure php approach is to set the cookie expiration to a time in the past.
setcookie('social', '', time() - 3600)
This assumes the cookie is being set on the same domain.
see http://php.net/setcookie

How to retrieve cookie value in CodeIgniter?

I can print the session values in codeigniter by print_r($this->session->userdata);
How can I print the cookies in codeigniter? I have just set a cookie:
$cookie = array(
'name' => 'test_cookie',
'value' => 'test',
'domain' => '/',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
How can i print the above cookie?
Look at the documentation: Codeigniter Cookie Helper Guide
It says that you should use $this->input->cookie() to retrieve a cookie:
$this->input->cookie('test_cookie', TRUE);
This worked for me on localhost, security might need tightened for server
$this->load->helper('cookie');
$cookie = array(
'name' => 'data',
'value' => '23',
'expire' => 86500,
'secure' => false
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('data', false));
Expire needs to be numeric, removed path and set secure to false
If you are using google chrome use inspect element to see if the cookie has been set... I think you can do it in FF, but I haven't used FF in a while... I only had one issue with cookies and that was I was setting domain to my live domain... So I have my cookie code like this:
$this->load->helper('cookie');
$cookie = array(
'name' => 'the_cookie',
'value' => 'test value here',
'expire' => '15000000',
'prefix' => ''
);
$this->input->set_cookie($cookie);
Here you can see it is showing up in Google Chrome "Inspect Element Tool"
'secure' => TRUE
This does not allow me to fetch the cookie.
just set
'secure' => FALSE
and see it may work.
setting the security => TRUE will not allow to print the cookie value in local, it only grant access to secure connections only so it will not print anything in localhost for you unless you set the security => FALSE
than using codeigniter CI_Input class you can get the value of cookie
$this->input->cookie('cookie_name', TRUE); //with xss filtering
$this->input->cookie('cookie_name'); //without xss filtering
If the code mentioned below does not provide any output, then modify the
application/config/config.php file and setting this:
$config['global_xss_filtering'] = TRUE;
$this->input->cookie('cookie_name', TRUE);
else just use this it will display the value
$this->input->cookie('cookie_name');
Load the cookie helper with:
$this->load->helper('cookie');
Then retrieve your cooking with:
$cookieData = get_cookie("cookie_name");
Note, these are aliases to using the input class, you can also retrieve and set cookies like so:
$cookieData = $this->input->get_cookie("cookie_name");
Source
http://ellislab.com/codeigniter/user-guide/helpers/cookie_helper.html

Categories