sessions are not working in codeignatior - codeigniter

I am set the array data in to sessions and redirect to search_results page but its not get the data
here i am set the data to sessions my sessions code is
$this->load->library('session');
$this->session->set_flashdata('data',$results);
redirect('search/search_results');
here retrive the sessions data code is
$this->load->library('session');
$myVar = $this->session->flashdata('data');
print_r($myVar);
but its not print the data
thanks for advance

I remember having the same problem with CodeIgniter, I think a year ago. I had the wrong cookie prefix in the application/config/config.php file. You could try setting it to its default, like:
$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
And then change it to your webserver settings if your webserver has other configuration.
CodeIgniter indeed stores session data in cookies as far as I know like prashant thakre mentioned in the comments on your question.
Good luck.

Related

CodeIgniter Cart and Session lost when refresh page

I'm using CodeIgniter v2.1.3 and having a problem with using CI Cart and Session. When I insert an element into Cart, everything gone fine. But when I refresh the page, all saved Cart items disappeared. The same problem happened when I use Session Class.
But everything works well on my localhost. The problem just happends on my Server.
There are some websites on my Server now and they dont have any problem with Session. So I guess it must be caused by CI.
Here is Session configuarations in application/config/config.php :
$config['sess_cookie_name'] = 'blowup_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions1';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
I tried to print the session_id but it returned nothing. So I guess the Session class did not generate any session_id. Try to start the session manually by using session_start(), the session_id was generated.
I also tried to save something by using $_SESSION, and they are saved without any problem.
Does it mean that the CI_Session and Cart library were not auto loaded?
How could I fix it? Or is there any Session class could replace the current one?
PS: My Server is running CentOS 5, PHP v 5.2.17 , Apache 2.2.23 and MySQL 5.0.96
UPDATED
Below is the function I use in Controller to add an item into Cart. The data ($params) is posted via an AJAX request (using jquery AJAX). The returned data is a HTML view.
public function add_to_cart(){
$this->layout->set_template('ajax');
if ($this->is_post()){
$params = $this->get_all_post_data();
//Debug::dump($this->cart);die;
if (isset($params['id']) && (int)$params['id']>0){
$product = $this->_product_model->get_record_by_id((int)$params['id']);
if (!is_null($product)){
if (count($this->cart->contents())>0){
foreach ($this->cart->contents() as $item){
if ($item['id']==$product->id){
$data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']);
$this->cart->update($data);
}else{
$data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title));
$this->cart->insert($data);
}
}
}else{
$data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title));
$this->cart->insert($data);
}
$this->session->set_userdata(array('test'=>'Session test'));
$this->layout->load('cart/topmenu_cart', $this->data);
}
}
}
}
Have you tried to use database to store session data instead? Based on your settings, I guess because your data is to large to hold in a 4KB cookie.

Session data lost so fast and rapidly in codeigniter

I have build a web application using codeignitet. It's about 200 users in my application. When user login, it success, but the session data just keep about one minutes and then kills automatically.
I use ci_sessions to store custom session data.
This is my session configuration :
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 900;
In my application, if session kill it redirect to login page. It always redirect and redirect to login page, i think it cause the session data lost rapidly. Need Helps and Thanks.
Your session's configuration looks Okay and you're also storing it in a database which is a good practice too.
It seems like is some place in your code you're destroying the session or part of it.
We have to see some code if you're unable to trace it yourself.
Good Luck!

GET params with URI rounting/ Codeigniter

$route['ajax/get/mail'] = "mail/get_mail_by_params";
I am trying to request *ajax/get/mail?user_id=123&foo=bar&bar=foo*
And params it in controller:
$foo = $this->input->get('foo')
But $_GET in ajax/get/mail variable is empty!
I suggest, that routing doesn’t supports GET paramets. What to do?
Have you tried using the MY_Input library? http://codeigniter.com/wiki/MY_Input/
Also, I think you may need to update your URI Protocol in config.php to PATH_INFO.
$config['uri_protocol'] = "PATH_INFO";

enable the query string in codeigniter

i already make two changes in config file to enable the $_GET array as
$config['uri_protocol'] = "PATH_INFO";
$config['enable_query_strings'] = true;
but whenever i try tow run my code as http://example.com/controller/function/$demo=demo
it redirected towards the default controller
Enabling query strings in CodeIgniter means that you're using the query string to pass in the controller and function instead of them being parsed from the PATH INFO.
If you want to use the default system for determining what controller and function to use, you do not want to set $config['enable_query_strings'] to true.
This previous SO post covers how to enable teh $_GET array in CodeIgniter: Enabling $_GET in codeigniter
I think that's what you're trying to do.
You also have to set controller and function triggers from config:
$config['enable_query_strings'] = TRUE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
So once query string is enabled, you can access it like this:
http://example.com/index.php?c=controller&m=function&demo=demo

How would i create a vanity url in codeigniter

How can i create a vanity url in codeigniter. Im having real trouble doing this in the framework. And there doesnt seem to be any good answers out there.
It's possible, I'm using this in one of my projects...
Here is a thread on the CodeIgniter forums showing how to do it...
http://codeigniter.com/forums/viewthread/113962/
According to Personalized User Vanity URL's in CodeIgniter, you can accomplish this by modifying your routes file:
$handle = opendir(APPPATH."/modules");
while (false !== ($file = readdir($handle))) {
if(is_dir(APPPATH."/modules/".$file)){
$route[$file] = $file;
$route[$file."/(.*)"] = $file."/$1";
}
}
/*Your custom routes here*/
/*Wrap up, anything that isnt accounted for pushes to the alias check*/
$route['([a-z\-_\/]+)'] = "aliases/check/$1";

Resources