这里有人熟悉CodeIgniter会话吗?我有一个功能登录脚本 . 成功登录后,将创建会话 . 用户被重定向到他们的主页 . 新页面应显示存储在会话变量中的用户名,但根本不显示任何内容 .
登录脚本中的代码:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
/**
* Index Page for this controller.
*/
public function index() {
$this->load->library('session');
$BB_DATA = array();
if(isset($_POST['login'])) {
// SELECT username, password FROM Bacon or bacon-like product. <-- Fuck you #nobanchancom
if(!empty($_POST['username']) && !empty($_POST['password'])) {
$q = sprintf('SELECT user_pk, username, pwhash, salt FROM tbl_user WHERE `username`=%s LIMIT 1;', $this->db->escape($_POST['username']));
$records = $this->db->query($q);
$username = $_POST['username']; // i added this
if($records->num_rows() == 1) {
$record = $records->row();
$test_hash = hash_password($record->salt . $_POST['password']);
if($record->pwhash === $test_hash) {
$new_salt = generate_salt();
$new_hash = hash_password($new_salt . $_POST['password']);
$q = sprintf('UPDATE tbl_user SET `pwhash`="%s", `salt`="%s" WHERE `user_pk`="%s" LIMIT 1;', $new_hash, $new_salt, $record->user_pk);
if($updates = $this->db->query($q)) {
$BB_DATA['messages']['information'][] = "Login successful.";
//add custom data to session
$this->session->set_userdata('username', "$username");
//$this->session->set_userdata('some_name', 'some_value');
// redirect to /users/home/
redirect(site_url() . 'users/home/'); exit();
} else {
$BB_DATA['messages']['critical'][] = "You have logged in successfully, but an error has occurred during login.";
}
} else {
$BB_DATA['messages']['critical'][] = "That username/password does not match.";
}
} else {
$BB_DATA['messages']['warning'][] = "That username/password does not exist.";
}
} else {
$BB_DATA['messages']['warning'][] = "You must enter a username/password.";
}
}
if(isset($_POST['register'])) {
// Redirect to /register/
redirect(site_url() . 'register/');
}
$this->load->view('login', $BB_DATA);
}
}
/* End of file login.php */
/* Location: ./application/controllers/login.php */
然后这是用户登陆页面的代码:
<?php
class Users extends CI_Controller {
public function home()
{
$this->session->userdata('username');
require_once(APPPATH . 'views/header.inc.php');//header
include(APPPATH . 'views/breadcrumbs.inc.php');//top breadcrumbs
echo ' <h1>Welcome'.$username.',</h1>';
//$username = $this->session->userdata('username');
echo $this->session->all_userdata();
//echo $username;
include(APPPATH . 'views/breadcrumbs.inc.php');//bottom breadcrumbs
require_once(APPPATH . 'views/footer.inc.php');//footer
}
}
?>
在登录脚本中,我调用codeigniters会话库 -
$ this-> load-> library('session');
稍后,假设一切正常,我将$ username变量添加到会话数据中 . 用户被重定向,并从那里
$ this-> session-> userdata('username');应该重新 Build 变量 . 它不是 .
建议?
注意:因此,我使用 print_r(array_values($this->session->all_userdata()));
来打印数组中的所有值 . 它包含:
数组([0] => 4c46f32b249ed55e8439152d63365fed [1] => 75.134.164.245 [2] => Mozilla / 5.0(Windows NT; Win64; x64; rv:26.0)Gecko / 20100101 Firefox / 26.0 Waterfox / 26.0 [3] = > 1410568024 [4] => [5] => ottomatic)
所以我很好奇,为什么把它添加为[5]而不是'用户名'?
1 回答
感谢来自@ xd6_的正确方向的一些提示,我看到了发生的事情 .
我误读了文档 - 您需要在每个页面上加载会话,除非它位于/config/autoload.php数组中 .
完成后,我能够按名称访问数组元素