QQAA2233 发表于 2013-8-1 21:20:58

请问2.1.3版本的session为什么不能跨控制器传输?

autoload.php:

/*
| -------------------------------------------------------------------
|Auto-load Libraries
| -------------------------------------------------------------------
| These are the classes located in the system/libraries folder
| or in your application/libraries folder.
|
| Prototype:
|
|    $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('session');


config.php中的encryption_key我已经设置了。

user.php:

<?php
/**
*    用户登录、注销、用户中心
*    @author Zhang Quan
*/
   
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class User extends CI_Controller {
      public function __construct() {
            parent::__construct();
            $this->load->model('home/User_model');
            $this->load->library('session');
      }
      
      /**
         *    通过 AJAX 验证登录与注册
         *
         */
      public function checkItems() {
            if ($this->input->is_ajax_request()) {
                $mode = $this->input->post('mode', TRUE);
                $username = $this->input->post('username', TRUE);
               
                if ($mode == 1) {            // 登录:验证用户名
                  $user = $this->User_model->getUser('username = '.$username);
                  if (empty($user)) {
                        echo 0;
                  } else {
                        echo 1;
                  }
                } else if ($mode == 2) {    // 登录:验证密码
                  $password = $this->input->post('password', TRUE);
                  $user = $this->User_model->getUser('username = '.$username.' and password = '.$password);
                  if (empty($user)) {
                        echo 0;
                  } else {
                        $this->session->set_userdata('userid', $user['id']);      // 存储用户 id
                        echo 1;
                  }
                } else if ($mode == 3) {    // 注册:验证用户名(必须唯一)
                  $user = $this->User_model->getUser('username = '.$username);
                  if (empty($user)) {
                        echo 1;
                  } else {
                        echo 0;
                  }
                } else if ($mode == 4) {    // 注册:验证邮箱(必须唯一)
                  $email = $this->input->post('email', TRUE);
                  $user = $this->User_model->getUser('email = '.$email);
                  if (empty($user)) {
                        echo 1;
                  } else {
                        echo 0;
                  }
                }
            }
      }
      /**
         *    登录
         *
         */
      public function login() {
            $userid = $this->session->userdata('userid');
            ($userid != 0)||die('登录失败');
            
            // 登录后获取相应用户的完整详细信息
            $user = $this->User_model->getUserInfo($userid);
            
            $this->session->set_userdata('user', $user);
            header('Location: http://192.168.255.128/chatroom/index.php/home/chat');
            exit();
      }
      
      /**
         *    通过 AJAX 验证注册
         *
         */
      public function regist() {
            if ($this->input->is_ajax_request()) {
               
            }
      }
      
      /**
         *    注销
         *
         */
      public function logout() {
            if ($this->session->userdata('username')) {
                $this->session->unset_userdata('username');
            }
            header('Location: http://192.168.255.128/chatroom/index.php/home/chat');
            exit();
      }
      
      /**
         *    用户中心
         *
         */
      public function ucenter() {
            
      }
    }



chat.php:

<?php
/**
*    主页
*    @author Zhang Quan
*/

    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
   
    class Chat extends CI_Controller {
      public function __construct() {
            parent::__construct();
      }
      
      public function index() {
            file_exists('chatAPP/views/home/Index/index.php')||show_404();
            
            echo $this->session->userdata('user');
            echo 'aa';
            
            //$this->load->view('home/Index/index.php');
      }
    }



我的功能是:登录时输入正确用户名与密码后,点击登录按钮就传值到user.php的login()中,然后从数据库中获得相关用户信息后传给一个叫“user”的SESSION中($this->session->set_userdata('user');),然后跳转回主页,并在主页中输出相应的SESSION信息(echo $this->session->userdata('user'))。但为什么传不了值到主页中呢?即在主页中读取不了SESSION为“user”的值呢?

dren_a 发表于 2013-8-2 11:12:17

是可以读的,你的用法也没错。

你用chrome或者firefox的firebug调试一下看看session中有没有成功设定相应的user的值吧。
页: [1]
查看完整版本: 请问2.1.3版本的session为什么不能跨控制器传输?