用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1009|回复: 1
收起左侧

[版本 3.x] 求指教:加载不同的页面,Session会变化?

[复制链接]
发表于 2016-5-28 19:50:26 | 显示全部楼层 |阅读模式
<?php
defined("BASEPATH") OR exit("No direct script access allowed");
/**
* admin of the content
*/
class Admin extends CI_Controller {
    private $signature;
    public function __construct()
    {
         parent::__construct();
         $this->load->helper("url_helper");
         $this->load->library("session");
         //session_start();
    }
   
    public function index()
    {
         /** login each time visit **/
       echo "Session id ".session_id();
       if (isset($_SESSION['user_name'])) {
            $this->display("welcom");
       } else {
           $this->display("login");
       }
    }
    public function login()
    {
           echo "Session id ".session_id();
           $_SESSION['user_name'] = $this->input->post("user_name");
           $this->display("welcom");
     }
    private function display($pages)
    {
          $this->load->view("templates/header", $data);
          $this->load->view("admin/$pages", $data);
          $this->load->view("templates/footer");
    }
}

上面是controller的源码,逻辑是这样,首次访问时,会调用index函数,因为$_SESSION['user_name'] 没有设置,会显示login页面。当Login提交后,设置$_SESSION['user_name'] 并显示welcom页面。
现在有个问题:
index时的Session ID和login页面提交时(login函数)的Session ID不一样,为什么?怎么保持一致?难道要在index中记录SessionID并传给login函数吗?

本版积分规则