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
Related
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
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
I have enabled CSRF Validation in Yii:
'enableCsrfValidation' => true,
Everything works as expected however I'd like for the session cookie to have the secure flag turned on.
With other cookies you can set the secure flag in the config:
'session'=>array(
'cookieParams' => array(
'httponly'=>true,
'secure' => true,
),
),
How do you do this for the YII_CSRF_TOKEN?
Add the following to your config:
'components' => array(
'request' => array(
'csrfCookie'=>array(
'secure'=>true,
),
),
),
You can't do that with the built in CHttpRequest component. You will need to derive from it and override the createCsrfCookie() to create a secure cookie as follows:
class CustomHttpRequest extends CHttpRequest {
protected function createCsrfCookie()
{
$cookie=new CHttpCookie($this->csrfTokenName,sha1(uniqid(mt_rand(),true)));
$cookie->secure = true; //Here is where you make your cookie secure
if(is_array($this->csrfCookie))
{
foreach($this->csrfCookie as $name=>$value)
$cookie->$name=$value;
}
return $cookie;
}
}
In your components configuration, specify your custom implementation:
'components'=>array(
....,
'request' => array(
'class' => 'CustomHttpRequest',
'enableCsrfValidation' => true,
),
IMPORTANT: For a new CSRF token to be generated, you will need to start a new browser session. Also, you will need to use HTTPS for a secure cookie to be in effect.
Delete all cookies for your development URI, or start a private session (in Chrome or Firefox) to start a new session.
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
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);