|
autoload.php:
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复制代码
<?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复制代码
<?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”的值呢?
|
|