【问题标题】:Codeigniter Session variable not set未设置 Codeigniter 会话变量
【发布时间】:2015-09-22 11:38:45
【问题描述】:

我正在尝试使用 $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();
  }  
}

?>

$booking_data 的我的var_dump 输出是:

array(1) {
    ["booking"]=> array(18) {
        ["car_id"]=> string(1) "7"
        ["car_slug"]=> string(19) "audi-r8-v10-spyder1"
        ["vehicle"]=> string(16) "Aston Martin DB9"
        ["start_location"]=> string(20) "premiere-velocity-hq"
        ["start_date"]=> string(8) "23/09/15"
        ["start_time_hour"]=> string(2) "10"
        ["start_time_min"]=> string(2) "00"
        ["return_location"]=> string(18) "birmingham-airport"
        ["end_date"]=> string(8) "24/09/15"
        ["end_time_hour"]=> string(2) "10"
        ["end_time_min"]=> string(2) "00"
        ["discount"]=> string(0) ""
        ["start_location_name"]=> string(20) "Premiere Velocity HQ"
        ["return_location_name"]=> string(18) "Birmingham Airport"
        ["start_date_time"]=> string(16) "23/09/15 at 1000"
        ["return_date_time"]=> string(16) "24/09/15 at 1000"
        ["total_days"]=> int(1)
        ["total_price"]=> string(8) "£744.00"
    }
}

【问题讨论】:

  • 您是否启用了会话?向我们展示您的php.ini 文件

标签: php codeigniter-2


【解决方案1】:

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

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

或者您可以将其添加到您的自动加载配置中,以便会话始终加载。请务必在配置中设置会话密钥,否则 CI 会话将无法工作。如果没记错的话。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多