Cookies not beeing set, codeigniter - codeigniter

I'm trying out cookies for first time in CI. Cant get it to work. Here's my code:
class Site extends CI_Controller {
public function index(){
$this->load->view('input_view');
$this->load->helper('cookie');
$this->test();
}
public function test(){
$cookie = array(
'name' => 'Test',
'value' => 'The Value',
'expire' => '86500',
//'domain' => '.some-domain.com',
'path' => '/',
'prefix' => 'myprefix_',
'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump(get_cookie('Test'));
}
var_dump... returns false. I understand it does this the first time but should'nt it be true after reloading the page? I can't see any "Test" cookie in my web tools in Chrome either. Do I need the "domain" row? I googled some and if I understood it was optional? If it isn't, what am i suppose to write there? Localhost? In my config file i have this settings for cookies
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;

I'm guessing you're not running https for development.
The secure flag on your cookie forces the cookie to be sent over HTTPS only;
Secure:
If this cookie can ONLY be sent over HTTPS (SSL), set this value to true.
Set it to false, and the cookie should work.

Related

Magento controller url redirects to dashboard

Basically in trying to create an inbox message which "read details" should redirect the user to a custom controller, however i can see the desired url in the browser for a second and then it redirects to the dashboard; this is how, currently, im trying to achieve that:
$myId = $myJson['id'];
$title = "Title of my notice";
$description = $myJson['text'];
$url= Mage::helper("adminhtml")->getUrl('My_Module/Controller/index', array('id' => $myId));
$sendingMessage = Mage::getModel('adminnotification/inbox')->addNotice($title,$description,$url);
The code above successfully adds the message to the inbox, however as i said before, i can see the desired URL in the browser before it gets redirected to the dashboard.
I'm accessing the same controller from another one and it does it as expected, the one that is actually working is a Grid and it looks something like this:
$this->addColumn('action',
array(
'header' => __('Answer'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => __('Answer'),
'url' => array('base'=> '*/Controller'),
'field' => 'id'
)),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
So, am i missing something here ?
BTW, is there any way to make the "read details" link to open in the same page instead of a new tab?
==================================================================
UPDATE
Disabling the "Add Secret Key to URLs" in the security options allowed me get it work, however i would like to make use of the secret keys.
The URLs i'm generating in the first code block actually have a key/value in the URLs, they look something like this:
https://example.com/index.php/mymodule/Controller/index/id/3963566814/key/f84701848a22d2ef36022accdb2a6a69/
It looks like you're trying to generate an admin URL. In modern versions of Magento, admin urls must use the adminhtml front name, using the Magento Front Name Sharing technique (described in this article). That's must as in if you don't, the URLs won't work. Magento removed the ability to create non-adminhtml URLs in the backend.
Second, here's where Magento generates the secret keys
#File: app/code/core/Mage/Adminhtml/Model/Url.php
public function getSecretKey($controller = null, $action = null)
{
$salt = Mage::getSingleton('core/session')->getFormKey();
$p = explode('/', trim($this->getRequest()->getOriginalPathInfo(), '/'));
if (!$controller) {
$controller = !empty($p[1]) ? $p[1] : $this->getRequest()->getControllerName();
}
if (!$action) {
$action = !empty($p[2]) ? $p[2] : $this->getRequest()->getActionName();
}
$secret = $controller . $action . $salt;
return Mage::helper('core')->getHash($secret);
}
and here's where it validates the secret key
#File: app/code/core/Mage/Adminhtml/Controller/Action.php
protected function _validateSecretKey()
{
if (is_array($this->_publicActions) && in_array($this->getRequest()->getActionName(), $this->_publicActions)) {
return true;
}
if (!($secretKey = $this->getRequest()->getParam(Mage_Adminhtml_Model_Url::SECRET_KEY_PARAM_NAME, null))
|| $secretKey != Mage::getSingleton('adminhtml/url')->getSecretKey()) {
return false;
}
return true;
}
Compare the pre/post hash values of $secret to see why Magento's generating the incorrect key on your page.

Not getting cookie value in codeigniter

Here is my code to set cookie
$cookie = array(
'name' => 'City',
'value' => $city,
'expire' => 86400 * 30,
'domain' => '.localhost',
'path' => '/',
'prefix' => 'picker_',
);
$this->input->set_cookie($cookie)
and i loading cookie helper in constructor of a controller.
my cookie is set form 1 month and i check in browser my cookie is set name
'City' and value = '10'. the problem is when i retrieve the value i get the blank page nothing is printing
here the code of fetching value
echo $this->input->cookie('picker_City');
i also try var_dump
var_dump($this->input->cookie('City', false));
it return false
what is the error.
You have to load the helper cookie first before using set_cookie
$this->load->helper('cookie');
and then use set_cookie
If you are using cookies very often , its better you load it in the autoload.php in the config folder.
in the autoload array
$autoload['helper'] = array('cookie');
Did you load it?
NOTE:
For local testing, leave the domain value blank.
'domain' => '',

How to work with CodeIgniter's cookie helper

This is my first post in stackoverflow and i'm really excited about that.
Actually i have problems with codeigniter cookie helper.
Take look at my codes:
$this->load->helper('cookie');
$cookie = array(
'name' => $nid,
'value' => 'checked'
);
$this->input->set_cookie($cookie);
echo $this->input->cookie();
// print_r($this->input->cookie();
as you see a declared an array called $cookie, then i set the cookie with that. but it seems that i made a mistake in setting cookies, because , the cookie doesn't set !
the immediate calling $this->input->cookie() returns nothing !
Does any one have any suggestion, where do i misunderstand the cookie helper of CI !?
var_dump() $this->input->set_cookie($cookie); // This returns NULL !
You need to give expiration time also in $cookie array and you are not echoing cookie what have you set. try this code
$this->load->helper('cookie');
$cookie = array(
'name' => $nid,
'value' => 'checked',
'expire' => '86500'
);
$this->input->set_cookie($cookie);
echo $this->input->cookie($nid, TRUE);

Codeigniter - cookies do not work in internet explorer 8

This code works in all browsers except Internet Explorer 8
$this->input->set_cookie(array(
'name' => 'test_cookie',
'value' => 'hello from cookie',
'expire' => 360000000,
'secure' => FALSE
));
echo get_cookie('test_cookie');
How to solve this problem? Why does not set_cookie?
I had a similar issue where only IE would refuse to accept a cookie. Turned out the computer's time zone wasn't set correctly (it was 17 hours ahead in the future, set to US PST while the server was in Australia), so what was happening is that the cookie was instantly expiring.
try:
echo $this->input->cookie('test_cookie');
I solved my problem using the function in helper
function setcookie_ex($name, $value, $expire)
{
$cookie_path = '/'; $cookie_domain = ''; $cookie_secure = false;
// Enable sending of a P3P header
header('P3P: CP="CUR ADM"');
if (version_compare(PHP_VERSION, '5.2.0', '>='))
setcookie($name, $value, $expire, $cookie_path, $cookie_domain, $cookie_secure, true);
else
setcookie($name, $value, $expire, $cookie_path.'; HttpOnly', $cookie_domain, $cookie_secure);
}

how to get and use codeigniter cookies?

I am trying to use codeigniter cookies so that i can bookmark some topic , i don't know how to get cookies vale and use it to display bookmarked item in the user browser , it doesn't work when i try to display cokies value using print_r(); please help how to proceed. Thanks
function index(){
$this->load->helper('cookie');
$data['title'] = 'some bookmark';
$cookie = array(
'name' =>'mycookie',
'value' => 'some val',
'expire' => '86500',
'domain' => '.localhost',
'path' => '/',
'prefix' => 'something_'
);
set_cookie($cookie);
$this->load->view('bookmark', $data);
}
Browsers have trouble dealing with localhost (or .localhost) as the domain for cookie storage - they expect a domain to have at least two dots. Try setting the domain to FALSE (or don't set it) while testing.
If that still doesn't work, can you show us how you're retrieving the cookie data?

Resources