我在登录时创建会话,但它没有保存我在会话中存储的变量,也没有将它们传递到其他页面:
控制器代码:
function CheckDatabase($password) //This function is only run when password validation is correct.//
{
$username = $this->input->post('Username'); //Sets the username as a $username.//
$result = $this->User_model->Login($username, $password);
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array( //Makes an array of the data to be stored in the session.//
'UserID' => $row->UserID,
'Username' => $row->Username
);
$this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
}
return TRUE;
}
else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.//
{
$this->form_validation->set_message('CheckDatabase', 'Invalid login details.');
return false;
}
}
同一控制器上的索引功能(应阻止用户返回登录界面,但不会)
function index() //Default function that is run when this controller is called.//
{
if($this->session->userdata('logged_in'))
{
redirect('Home_controller');
}
else
{
$this->load->helper(array('form'));
$this->load->view('Login_view');
}
}
主视图上的代码(登录后指向哪个浏览器)
<?php echo $UserID .": ".$Username; ?>
我因显示会话数据而收到此错误:
消息:未定义的变量:用户名
文件名:views / Home_view.php
行号:9
答案 0 :(得分:2)
要检索会话变量,您必须这样做
{{1}}
答案 1 :(得分:1)
在使用之前,你必须从这样的会话中检索它:
$user = $this->session->userdata('logged_in');
$Username = $user['Username'];
$UserID = $user['UserID'];
答案 2 :(得分:0)
试试这个
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct() {
parent::__construct();
//Here we will check for the session value if it is true then the function will run else it will redirect to login page
if ($this->session->userdata('logged_in') == FALSE) {
redirect(site_url('your login page'));
} else {
// if there is session value
redirect(site_url('User/your function name');
}
}
}
?>
并检查控制器构造中的用户会话
function compChoice() {
compMath = Math.floor(Math.random() * 3) + 1;
switch (compMath) {
case 1:
pick = "Rock"
break;
case 2:
pick = "Paper"
break;
case 3:
pick = "Scissors"
break;
}
return pick;
}
function compChoice() {
var compMath = Math.floor(Math.random() * 3) + 1;
switch (compMath) {
case 1:
pick = "Rock"
break;
case 2:
pick = "Paper"
break;
case 3:
pick = "Scissors"
break;
}
return pick;
}
function vsChoice() {
if (player === "Rock") {
if (computer === "Scissors") {
console.log("Win.");
} else if (player === computer) {
console.log("Tie.");
} else {
console.log("Lose.");
}
}
if (player === "Paper") {
if (computer === "Rock") {
console.log("Win.");
} else if (player === computer) {
console.log("Tie.");
} else {
console.log("Lose.");
}
}
if (player === "Scissors") {
if (computer === "Paper") {
console.log("Win.");
} else if (player === computer) {
console.log("Tie.");
} else {
console.log("Lose.");
}
}
}
$('#box').on('click', function() {
player = compChoice();
computer = compChoice();
console.log("Players Choice: " + player);
console.log("Computer Choice: " + computer);
vsChoice();
});
有关完整的解释,请阅读本教程
答案 3 :(得分:0)
if($result)
{
$sess_array = array();
foreach($result as $row)
{
$sess_array = array( //Makes an array of the data to be stored in the session.//
'UserID' => $row[0]->UserID,
'Username' => $row[0]->Username
);
$this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
}
return TRUE;
}