Calling out cookie value (codeigniter) - codeigniter

I have a cookie that I set from domain A and trying to retrieve the value at domain B . Here is how I set my cookie from domain A.
$cookie = array(
'name' => 'userId',
'value' => $_SESSION['user']->id,
'expire' => time() + 3600,
'domain' => 'example.com',
'path' => '/',
'prefix' => 'mycookie_',
'secure' => FALSE,
);
$this->input->set_cookie($cookie);
So when I do var_dump($this->input->cookie('mycookie_userId')); it will return me the value like this string(2) "78"
From domain B I tried to print out the value var_dump(get_cookie('mycookie_userId')); but it return NULL.

I found the mistake. When setting the domain name I set it like example.com then I changed it to .example.com adding the dot in front then it working fine now.

Related

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 echo config item from config file for view in Codeigniter?

How to call config item from config file for view in codeigniter.
here is my config file
$config['user'] = array(
'email_validation' => 'email validation',///^([\w-]+(?:\.[\w-]+)*)#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
'no_permission' => '/',
'admin_group' => 'admin',
'default_group' => 'default',
'public_group' => 'public',
'users' => 'aauth_users',
'groups' => 'aauth_groups',
'user_to_group' => 'aauth_user_to_group',
'perms' => 'aauth_perms',
'perm_to_group' => 'aauth_perm_to_group',
'perm_to_user' => 'aauth_perm_to_user',
'pms' => 'aauth_pms',
'system_variables' => 'aauth_system_variables',
'user_variables' => 'aauth_user_variables',
'remember' => ' +3 days',
'max' => 13,
'valid_chars' => array(' ', '\''),
'ddos_protection' => true,
'recaptcha_active' => false,
'recaptcha_login_attempts' => 4,
'recaptcha_siteKey' => '',
'recaptcha_secret' => '',
'max_login_attempt' => 10,
'verification' => false,
'email' => 'admin#admin.com',
'name' => 'Emre Akay'
);
Here is my load config
$this->config->load('user');
And I will view its item for view as below
$site_name = $this->config->item('email_validation');
But is don't show any thing
This is because your config array is two dimensional array. So, you can't access directly email_validation without getting user first. Moreover,
$this->config->load('user'); just means loading user.php from application/config/ directory. Doesn't mean loading user index from $config array. You can do it like that.
$userConfig = $this->config->item('user');
echo $userConfig["email_validation"];
Edit
Please make sure you config file is under application/config/ and loaded.
$this->config->load('user');
You can check which config is loaded by doing like this.
echo "<pre>";
print_r($this->config);
echo "</pre>";
Hope it will be useful for you.
if your php version>=5.4 you can use this
$site_name = $this->config->item('user')['email_validation']

CodeIgniter get_cookie / input->cookie not retrieving

I am setting a cookie like so:
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500,
'domain' => '.iwantaspeaker.com',
'path' => '/'
//'secure' => TRUE
);
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('test', false));
Which returns bool(false)
I get absolutely no response. I have in the config:
$config['cookie_prefix'] = "iwas_";
And the cookie is stored as iwas_test, so I have also tried $this->input->cookie("iwas_test",true); to no avail.
I can also see that the cookie is set in chrome:
Furthermore, I have also tried using the cookie helper. What must I do to retrieve the cookie correctly? the URL is on a local network machine, so the domain is pointed to the local IP with an entry in my hosts file, if this makes any difference.
ah-ha! make sure you dont pass expire as a string
$cookie = array(
'name' => 'test',
'value' => 'test',
'expire' => 86500, <--
'domain' => 'www.iwantaspeaker.com',
'path' => '/',
'secure' => TRUE <-- will only be set on https
);
// $this->ci->db->insert("UserCookies", array("CookieUserEmail"=>$userEmail, "CookieRandom"=>$randomString));
$this->input->set_cookie($cookie);
var_dump($this->input->cookie('iwas_test', false));

cookie not being set straight away

I have a basket cookie, before adding a product to the basket the function checks to see if a cookie exists or if a new cookie is required. This works fine when adding one item at a time but there are occasions when more than one item is added at the same time. Again if an old basket cookie exists this works fine. The problem is when no basket cookie exists and the function had to create one.
The first time it goes through, the cookie is created a database record is added ect.
The second time round the function we check for the cookie no cookie is found and another cookie is created etc.
$this->db->select('basket_id');
$this->db->from($this->basket_table);
$this->db->where('basket_id', get_cookie('basket_id'));
$check_basket = $this->db->get();
if($check_basket->num_rows > 0){
$basket_exists = 1;
}else{
$basket_exists = 0;
}
if($basket_exists == 0){
delete_cookie('basket_id');
$basket = array(
'lang_id' => $lang_id,
'currency_id' => $currency_id,
'customer_id' => $customer_id,
);
$this->db->insert($this->basket_table, $basket);
$basket_id = $this->db->insert_id();;
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 60*60*24*30,
'domain' => 'REMOVED'
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
}else{
$basket_id = get_cookie('basket_id');
}
In order to set the cookie, you need to send it to the browser, - but this never happens if your function loops multiple times before you create a view.
So set the cookie prior to them using the basket, OR only check if the cookie needs to be set once, like this:
$this->db->select('basket_id');
$this->db->from($this->basket_table);
$this->db->where('basket_id', get_cookie('basket_id'));
$check_basket = $this->db->get();
if($check_basket->num_rows > 0) {
$basket = array(
'lang_id' => $lang_id,
'currency_id' => $currency_id,
'customer_id' => $customer_id,
);
$this->db->insert($this->basket_table, $basket);
$basket_id = $this->db->insert_id();
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 60*60*24*30,
'domain' => 'REMOVED'
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
}
// Now run your basket logic here - knowing the cookie is setup
}

codeigniter cookie expiry problem

I'm having a cookie issue, the expiry date on my cookie is always being set to At End Of Session which isn't what I want. I did a bit of goggling and it suggested it set the expire to time()+60*60*24*30 which I've done.
//Create basket cookie
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => time()+60*60*24*30,
'domain' => 'domain',
'path' => '/',
'prefix' => '',
);
set_cookie($cookie);
I did wonder if it could be down to a Codeignter setting but my ci_session cookie has a normal expiry date. Thu, 09 Jun 2011 10:39:02 GMT
This is what I get when I view the cookie:
Name basket_id
Value 28
Host .host
Path /
Secure No
Expires At End Of Session
And here is an example of the array I'm passing to the cookie.
Array ( [name] => basket_id [value] => 30 [expire] => 1310202067 [domain] => host [path] => / [prefix] => )
Your expiry date is set incorrectly. You don't have to include the time(), as what you're setting is actually the expiry date from time().
When you have an incorrect expire value, it defaults to 0, which is set as your session's length instead.
Therefore it should be:
$cookie = array(
'name' => 'basket_id',
'value' => $basket_id,
'expire' => 86400*30,
'domain' => 'domain',
'path' => '/',
'prefix' => '',
);
Please check out the answer below by #Gowri for how to do it properly.
You can try to adjust session expiration time in config.php CI session initially is saved in cookies:
/** Session Variables
---------------------------------------
| 'session_expiration' = the number of SECONDS you want the session to last.
| by default sessions last 7200 seconds (two hours). Set to zero for no expiration.
|
*/
$config['sess_expiration'] = 7200;
You can add params
$config['cookie_lifetime'] = 1800
in config.php, the reason you can find in libraries/Sessions/Session.php, code below
$expiration = config_item('sess_expiration');
if (isset($params['cookie_lifetime']))
{
$params['cookie_lifetime'] = (int) $params['cookie_lifetime'];
}
else
{
$params['cookie_lifetime'] = (!isset($expiration) && config_item('sess_expire_on_close'))
? 0 : (int) $expiration;
}

Resources