未设置Codeigniter会话变量

时间:2015-09-22 11:38:45

标签: php codeigniter-2

我正在尝试使用$ this-> session-> set_userdata($ booking_data)设置会话变量,但它不会设置会话变量。我的控制器是:

<?php

class Car extends CI_Controller {

    function __construct()
    {       
        parent::__construct();

        $this->load->model(array('Tariff_model', 'Category_model', 'Brand_model', 'Car_model', 'Booking_model'));
        $this->load->helper('formatting_helper');
    }

    function index($car_id = 0)
    {
        //price calculation
        $delivery_charge = $data['locations'][$this->input->post('start_location')]['delivery_charge'];
        $collection_charge = $data['locations'][$this->input->post('return_location')]['collection_charge'];
        $tax_vat = config_item('vat')/100;

        $booking_price = ($car->price_3*$total_days) + $delivery_charge + $collection_charge;
        $booking_price_vat = $booking_price*$tax_vat;                   
        $booking_total_price = $booking_price+$booking_price_vat;

        $booking_data = array(
            'car_id' => $car_id,
            'car_slug' => $car->slug,
            'vehicle' => $car->title,
            'start_location' => $this->input->post('start_location'),
            'start_date' => $this->input->post('start_date'),
            'start_time_hour' => $this->input->post('start_time_hour'),
            'start_time_min' => $this->input->post('start_time_min'),
            'return_location' => $this->input->post('return_location'),
            'end_date' => $this->input->post('end_date'),
            'end_time_hour' => $this->input->post('end_time_hour'),
            'end_time_min' => $this->input->post('end_time_min'),
            'discount' => '',
            'start_location_name' => $data['locations'][$this->input->post('start_location')]['name'],
            'return_location_name' => $data['locations'][$this->input->post('return_location')]['name'],
            'start_date_time' => $this->input->post('start_date').' at '.$this->input->post('start_time_hour').$this->input->post('start_time_min'),
            'return_date_time' => $this->input->post('end_date').' at '.$this->input->post('end_time_hour').$this->input->post('end_time_min'),
            'total_days' => $total_days,
            'total_price' => format_currency($booking_total_price)
        );

        $this->Booking_model->set_booking_data($booking_data);

    }
}

?>

和Booking_model.php:

<?php
Class Booking_model extends CI_Model
{

  function __construct()
  {
    parent::__construct();
  }

  function reset_booking_data()
  {
    $this->session->unset_userdata('booking');
  }

  function set_booking_data($data)
  {
    $booking_data = array();
    $booking_data['booking'] = array();
    $booking_data['booking'] = $data;

    // put our booking in the cart
    $this->session->set_userdata($booking_data);
  }

  function save_booking_data($data)
  {
    if(isset($data['id']))
    {
      $this->db->where('id', $data['id']);
      $this->db->update('orders', $data);
      return $data['id'];
    }
    else
    {
      $this->db->insert('orders', $data);
      return $this->db->insert_id();
    }
  }

  function get_booking_data()
  {
    $booking = $this->session->userdata('booking');

    return $booking;
  }

  function is_booking_data()
  {    
    $booking = $this->session->userdata('booking');

    if(!$booking)
    {
      return false;
    }
    else
    {
      return true;
    }
  }

  function orders($data=array(), $return_count=false)
  {
    if(empty($data))
    {
      //if nothing is provided return the whole shabang
      $this->get_all_orders();
    }
    else
    {
      //grab the limit
      if(!empty($data['rows']))
      {
        $this->db->limit($data['rows']);
      }

      //grab the offset
      if(!empty($data['page']))
      {
        $this->db->offset($data['page']);
      }

      //do we order by something other than category_id?
      if(!empty($data['order_by']))
      {
        //if we have an order_by then we must have a direction otherwise KABOOM
        $this->db->order_by($data['order_by'], $data['sort_order']);
      }

      if($return_count)
      {
        return $this->db->count_all_results('orders');
      }
      else
      {
        return $this->db->get('orders')->result();
      }

    }
  }

  function get_all_orders()
  {
    //sort by alphabetically by default
    $this->db->order_by('id', 'DESC');
    $result = $this->db->get('orders');

    return $result->result();
  }  
}

?>

var_dump的{​​{1}}输出为:

$booking_data

1 个答案:

答案 0 :(得分:0)

您需要初始化会话库以启动CI会话。在控制器中使用它。

$this->load->library('session');

或者您可以将其添加到自动加载配置中,以便会话始终加载。请务必在配置中设置会话密钥,否则CI会话将无法运行。如果记忆对我有用。