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.
Related
How can set auto route 404 page of frontend and backend different ? I have searched a long time but i have not found perfect answer.
Btw, can we make the same for error 500 ?
You can easily do it by using conditional statement.
go to application/config/routes.php and remove:
$route['404_override'] = '';
After that add the following code.
$req_uri = $_SERVER['REQUEST_URI']; // $req_uri = /myproject/backend
$req_uri = explode('/', $req_uri);
$req_uri = $req_uri[2]; // $req_uri[2] = backend
if($req_uri == 'backend'){
$route['404_override'] = 'Backend_error'; // Not found controller for backend
}else {
$route['404_override'] = 'Frontend_error'; // Not found controller for frontend
}
You can use echo statement to analyze further. and then do more stuff accordingly.
I have strange problem with sessions. In my small admin panel for my site there were some pages, 6 in fact. All worked nice. But in time I added one more page and when I go to that one firstly I see the content and after refreshing page or going to another one I'm thrown out from the account. Cookies are not deleted. It seems that session is destroyed but there is no any work with sessions. That is just infomartion page where some data from db is displayed.
This is code from index.php where session starts:
ini_set('session.gc_maxlifetime', 3600);
ini_set('session.cookie_lifetime', 3600);
session_start();
...
And this code from problem page 'orders.php':
$res = $administrator->getOrders();
while($order = mysql_fetch_array($res, MYSQL_ASSOC)) {
filtrate and print data from db in a table
}
Do you have any assumptions? I have not, was searching for an error for 2 days ;(
Check authorization function:
public function checkLogin() {
if(isset($_SESSION['hash']) && isset($_SESSION['id'])) {
$id = intval($_SESSION['id']);
$where = "`id`='$id'";
$cookie = DBWorking::getFieldWhere('cookie', 'users', $where);
$hash = mysql_fetch_assoc($cookie);
if($_SESSION['hash'] === $hash['cookie']) {
return true;
}
}
return false;
}
The session value lost after the redirect. I m redirecting from pay_order method to do_payment method. when I print the session value in do_payment method its return false. just simple calling the do_payment method display the session value. Please help what is the problem with redirect..?
function pay_order($order_id)
{
$this->load->helper('url'); //loading url helper
$this->load->library('session');//loading session lib
$this->load->library('cart'); //loading cart lib
$this->load->helper('form');//loading form helper
$output = $this->cart->contents();// getting data from cart.
$output = $this->sort_array($output);// sorting the array
$list['data'] = $output;
$list['order_id'] = $order_id;
$this->session->set_userdata('abc', $list);// setting the session
redirect('checkout/do_payment'); // redirecting to do_payment
}
function do_payment()
{
$this->load->helper('url'); //loading url helper
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
var_dump( $arr );// return false value.
//$this->load->view('order/pay_through_gateway');
}
when I redirect from pay_order method the session is not available in do_payment method. why?
There is a problem, check the line in the method pay_order() where you are setting the session.
$list['order_id'] = $order_id
$this->session->set_userdata('abc', $list);// setting the session
Before that line you have missed semicolon. And everything looks fine.
Update:
There is no error as I think, check whether you may have null value while you are storing the values in session. And check the values by following code in do_payment() method.
function do_payment(){
$this->load->library('session'); //loading session library
$arr = $this->session->userdata('abc');// getting session data in $arr
print_r( $arr );// print the $arr content
}
Change this:
$list['order_id'] = $order_id
To this:
$list['order_id'] = $order_id;
You have a semicolon missing. You do not see any errors because of the redirection.
I have tried setting cookies in codeigniter like this, but not working don't know what is the problem
//setting cookie
$this->load->helper('cookie');
if($loaded_position == 'nativelocation' ){
$this->input->set_cookie('nativelocation' , $request_place, '86400');
//echo $this->input->cookie('nativelocation');
}
//retrieving cookie
$this->load->helper('cookie');
$savedlocation = array();
if( $this->input->cookie('addlocation_1') )
{
$savedlocation[]= $this->input->cookie('addlocation_1') ;
}
It looks like you are setting a cookie called "nativelocation" but then you try to retrieve a cookie called "'addlocation_1", and there isn't one. Try:
if( $this->input->cookie('nativelocation') ){
echo $this->input->cookie('nativelocation');
}
Also, know that when setting a cookie, you can not access it on the same "page" you'll need a page refresh, or access the cookie $this->input->cookie('nativelocation') on another page/controller.
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!