1

我成功设置了会话,因为输出分析器向我显示了会话名称和值。但是当我 POST 数据控制器无法接收会话数据时。库已加载,$config['sess_expire_on_close'] = TRUE 我已更改 TRUE-FALSE,但没有任何成功。还尝试重写代码。还有一个问题,我使用两台 PC,在 Linux 机器上我收到错误“标头已发送...”,但在 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
      }
4

3 回答 3

1

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

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

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

于 2013-04-11T10:25:33.333 回答
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
     }
于 2013-04-11T10:12:38.500 回答
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

?>

查看授权:

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

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


?>

html: 表单方法="post" action="authorisation_user" 输入类型="text" name="surname"

查看视频头:

<?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');
   ?>
于 2013-04-11T11:22:05.450 回答