笨。控制器无法接收会话数据

时间:2013-04-11 07:42:04

标签: codeigniter session

我成功设置会话,因为输出探查器显示会话名称和值。 但是当我POST数据控制器时无法接收会话数据。 库被加载,$ config ['sess_expire_on_close'] = TRUE我已经改变了TRUE-FALSE而没有任何成功。还试过重写代码。 另一个问题我使用两台PC而在Linux机器上我收到错误“Header already sent ...”,但在Win机器上我没有收到此消息。如何在Win PC上启用它。通知和警告已启用。

所以......

控制器kmgld:

  function authorisation_user()
     {
          ......
          $data['set_cookie']       = "Surname";
          ......
          $this->load->view('vheader', $data);
          $this->load->view('vuser_kmgld');
          $this->output->enable_profiler(TRUE); //show me only session name and value which I set
     }

查看:

  if ($set_cookie!=NULL)
   {
        $this->session->set_userdata('surname',$set_cookie);

    }
  <!Doctype...>

  <form action="<?php echo base_url()?>index.php/kmgld/update_kmgld" method="post" name="">

再次控制器kmgld

     function update_kmgld()
      {
       ...update DB
      $test=$this->session->userdata('surname');
        echo $test; //it is NULL
       $this->output->enable_profiler(TRUE); // show me only now session id, ip, user agent
      }

3 个答案:

答案 0 :(得分:1)

您必须在控制器中设置userdata,视图不适合它。所以你可能会在你的控制器中做这样的事情:

$surname = "Surname";
$this->session->set_userdata('surname',$surname);
$data['set_cookie'] = $surname;
...
$this->load->view('vheader', $data);

不知道您是否自动加载会话库。否则你必须在你需要的每个功能中加载它。

答案 1 :(得分:0)

从视图中删除会话设置并在控制器中执行:

function authorisation_user()
     {

          $data['set_cookie']       = "Surname";

          $this->session->set_userdata('surname',$set_cookie);
          $this->load->view('vheader', $data);//are you sure here is where $data should go ?
          $this->load->view('vuser_kmgld');//not $data here?
          $this->output->enable_profiler(TRUE); //show me only session name and value which I set
     }

答案 2 :(得分:0)

控制器:

 <?php

 class Kmgld extends CI_Controller {

function index()
    {   
        $data['flag'] = "first";
        $this->load->model('Mkmgld');   
        $this->load->view('vheader');
        $this->load->view('vauthorisation',$data);
        $this->load->view('vfooter');

    }

function get_kmgld()
    {
        $this->load->model('Mkmgld');   
        $this->Mkmgld->get_kmgld();
        $this->load->view('vheader');
        $this->load->view('kmgld');
        $this->load->view('vfooter');
    }

function authorisation_user()
    {
        $this->load->model('Mkmgld');
        $surname_session            = $this->session->userdata('surname');
        $data['surname_post']       = mb_convert_case($this->input->post('surname'), MB_CASE_TITLE, "UTF-8"); 
        $data['user_id']            = $this->Mkmgld->valid_user($data['surname_post']);
        $surname                    = (isset($data['user_id'][0]->surname)? $data['user_id'][0]->surname: "");

        if(isset($surname) and $surname !=NULL)
            {   
                $data['query']              = $this->Mkmgld->get_kmgld($data['surname_post']);
                $data['get_trip_target_id'] = $this->Mkmgld->get_trip_target_id();
                $data['set_cookie']         = $data['surname_post'];
                $this->session->sess_destroy();

                $this->load->view('vheader', $data);
                $this->load->view('vuser_kmgld');
                $this->load->view('vfooter');

            }else if (isset($surname_session) and $surname_session!= NULL)
                    {   
                        //echo "you are in session";
                        $data['query']              = $this->Mkmgld->get_kmgld($surname_session);
                        $data['get_trip_target_id'] = $this->Mkmgld->get_trip_target_id();

                        $this->load->view('vheader', $data);
                        $this->load->view('vuser_kmgld');
                        $this->load->view('vfooter');

                    } else
                            {
                                    $data['flag']   =   "wrong";
                                    $this->load->view('vheader');
                                    $this->load->view('vauthorisation',$data);
                                    $this->load->view('vfooter');
                            }

                //echo "<pre>";
                //var_dump($data);
           $this->output->enable_profiler(TRUE); 
    }//end authorisation_user()

function update_kmgld()
    {
        $this->load->model('Mkmgld');
        $data['get_trip_target_id'] = $this->Mkmgld->get_trip_target_id();
        $trip_target_id             = $data['get_trip_target_id'][0]->Auto_increment;
        $this->Mkmgld->update_kmgld($this->input->post('day')
                                    ,$this->input->post('mon')
                                    ,$this->input->post('year')
                                    ,$this->input->post('spd_before')
                                    ,$this->input->post('spd_after')
                                    ,$this->input->post('total')
                                    ,$this->input->post('target')
                                    ,$this->input->post('approved')
                                    ,$this->input->post('user_id')
                                    ,$trip_target_id);
        $a=$this->session->userdata('surname');
        if ($a==NULL)
            {
                echo $a;
                //redirect('kmgld/authorisation_user');
                $this->output->enable_profiler(TRUE); 
            }


    }
     }//end class kmgld
     ?>

型号:

enter code here<?php

类Mkmgld扩展了CI_Model {

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

function get_kmgld($surname){
        $query = $this->db->query("SELECT
                                            *
                                    FROM `user`
                                    INNER JOIN `user_has_trip`
                                    ON `user`.`user_id` = `user_has_trip`.`user_id`
                                    INNER JOIN `trip_target`
                                    ON `user_has_trip`.`user_has_trip_id` = `trip_target`.`trip_target_id`
                                    WHERE `user`.`surname` = '$surname'
                                ");
        return $query->result(); 

}

function valid_user($surname)
    {
        $user_id = $this->db->query("SELECT
                                            *
                                    FROM `user`
                                    WHERE `user`.`surname`='$surname'
                                ");
        return $user_id->result();
    }


function get_trip_target_id()
    {
        $get_trip_target_id = $this->db->query("SHOW TABLE STATUS LIKE 'trip_target'");
        return $get_trip_target_id->result();

    }

function update_kmgld($day, $mon, $year, $spd_before, $spd_after, $total, $target, $approved, $user_id, $trip_target_id)
    {
        $date = $year."-".$mon."-".$day;
            $this->db->query("INSERT INTO `trip_target` (`trip_target_id`
                                                  ,`date`
                                                  ,`speedometer_before`
                                                  ,`speedometer_after`
                                                  ,`duration`
                                                  ,`target`
                                                  ,`approved`)
                             VALUES (NULL
                                     ,'$date'
                                     ,'$spd_before'
                                     ,'$spd_after'
                                     ,'$total'
                                     ,'$target'
                                     ,'$approved')
                            ");

            $this->db->query("INSERT INTO `user_has_trip` 
                                            (`user_has_trip_id`
                                                ,`user_id`
                                                ,`trip_target_id`
                                            )
                                      VALUES  (NULL
                                               ,'$user_id'
                                               ,'$trip_target_id'
                                               ) 
                             ");


    }


}//end class

?>

查看vauthorisation:

<?php
    $surname_value = $this->session->userdata('surname');
?>

   <?php
   if ($flag =="wrong")
{
    echo "...Bad very bad. Try use another language ";
}


?>

HTML: form method =“post”action =“authorisation_user”         input type =“text”name =“surname”

查看vheader:

<?php
 if ($set_cookie!=NULL)
{
    $this->session->set_userdata('surname',$set_cookie);
    echo "cookie set".$set_cookie;

}


?>

查看vuser_kmgld:

HTML: form action =“update_kmgld” 输入name = day,name = mon,name = year,name = spd_before ...等。在PHP代码之后:

if (isset($user_id[0]->user_id))
  {
     foreach ($query as $row)
{ 
    echo "<tr>
                                <td>".(isset($row->date)? date("d.m.Y",     strtotime($row->date)): "")."</td>
                    <td>". (isset($row->speedometer_before)? $row->speedometer_before : "")."</td>
                    <td>". (isset($row->speedometer_after)? $row->speedometer_after : "")."</td>
            <td>". (isset($row->duration)? $row->duration : "")."</td>
            <td>". (isset($row->target)? $row->target : "")."</td>
            <td>". (isset($row->aproved)? $row->aproved : "")."</td>
        </tr>";
    }

   } //else redirect('kmgld/index');
   ?>