我对Codegniter框架很新。我想在我的网站上设置一个会话,但是当我点击浏览器的后退按钮时,它会进入我网站的主(登录)页面,当我点击下一步时,页面显示未找到。此外,在欢迎页面上,当我点击它时,我还有一个用户按钮,它会将我带到我的登录页面,但会话数据仍然存在。
以下是我的控制器的代码(Welcome.php):
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$this->home();
}
public function home()
{
if($this->session->all_userdata('is_logged_in')==1){
$this->load->view('welcome_message');
}
else{
$data['title'] = 'ABC';
$this->load->view('login', $data);
}
}
function loginapi()
{
if ($this->session->all_userdata('is_logged_in')==1) {
$this->load->view('welcome_message', $this->session->all_userdata);
}
else{
$this->load->library('form_validation');
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('password','Password','required');
if ($this->form_validation->run()) {
$username = $this->input->post('username');
$password = $this->input->post('password');
$payload = json_encode( array( "username"=> $username,
"password"=> $password ) );
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL,
'../api/dashboard/login');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $payload);
$buffer = curl_exec($curl_handle);
$httpcode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
var_dump($httpcode);
$result = json_decode($buffer);
var_dump($result->empid);
if($httpcode == 200)
{
echo 'User has been updated.';
$this->load->driver('session');
//return $result;
$data['empid'] = $result->empid;
$data['username']=$this->input->post('username');
$data['password']=$this->input->post('password');
$data['title'] = 'ABC';
$data['is_logged_in'] = '1';
$this->session->set_userdata($data);
$this->load->view('welcome_message', $data);
}
else
{
echo 'Something has gone wrong';
}
}
else{
$this->load->view('login');
}
}
}
function getdata()
{
if ($this->session->all_userdata('is_logged_in')==1) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL,
'../api/dashboard/getUser');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($curl_handle);
$httpcode = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
curl_close($curl_handle);
var_dump($httpcode);
$result = json_decode($buffer);
var_dump($result);
if($httpcode == 200)
{
//return $result;
$data['response'] = $result;
$this->load->view('hr', $data);
}
else
{
echo 'Something has gone wrong';
}
}
else{
$this->load->view('login');
}
}
public function logout(){
$this->session->sess_destroy();
redirect('welcome/home');
}
}
我的会话数据如下所示:
{
'__ci_last_regenerate' => int 1493105150
'empid' => string '13' (length=2)
'username' => string 'user@example.xyz' (length=18)
'password' => string 'password' (length=6)
'title' => string 'ABC' (length=12)
'is_logged_in' => string '1' (length=1)
}
我的login.php查看:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
<?php echo $title; ?>
</title>
</head>
<body>
<div id="container">
<h1>
<?php echo "login"; ?>
</h1>
<?php
echo form_open('welcome/loginapi');
echo validation_errors();
echo "<p>Username: ";
echo form_input('username');
echo "</p>";
echo "<p>Password: ";
echo form_password('password');
echo "</p>";
echo "<p>";
echo form_submit('login_submit','Login');
echo "</p>";
echo form_close();
var_dump($this->session->all_userdata());
?>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong>
seconds.
<?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter
Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
我的welcome_message.php视图
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<div id="container">
<h1><?php echo "welcome page"; ?></h1>
<div id="body">
<?php
//var_dump($empid);
var_dump($this->session->all_userdata('password'));
echo form_open('welcome/getdata');
echo form_hidden('_hidden_field',$this->session-
>all_userdata('empid'));
echo "<p>";
echo form_submit('get_user','Users');
echo "</p>";
echo form_close();
?>
<?php
echo form_open('welcome/logout');
echo "<p>";
echo form_submit('logout','Logout');
echo "</p>";
echo form_close();
?>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong>
seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version
<strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
编辑: 当我点击用户按钮时,它通过getdata函数将我带到hr.php,当我回去时它带我到welcome_message.php,但是当我按下前进按钮时它来自welcome_message.php它告诉我重新提交
这是我的hr.php视图:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body onunload="">
<div id="container">
<h1><?php echo "Human Resource Management"; ?></h1>
<div id="body">
<div class="row">
<div class="col-lg-12">
<h1>Human Resource Management</h1>
<?php
echo form_open('welcome/registeruser');
echo "<p>";
echo form_submit('get_user','Register');
echo "</p>";
echo form_close();
?>
<br>
<br>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class= "panel panel-default">
<div class="panel-heading">
<h4>Registered Employees</h4>
</div>
<div class="panel-body">
<table class="table table-hover">
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
<th>E-mail</th>
<th>Contact No.</th>
<th>Username</th>
<th>Emp ID</th>
<th>Department</th>
<th>Designation</th>
</tr>
</thead>
<tbody>
<?php
foreach ($response as $object) {
?>
<tr>
<td><?php echo $object->firstname; ?> <?php echo
$object->lastname; ?></td>
<td><?php echo $object->gender; ?></td>
<td><?php echo $object->email; ?></td>
<td><?php echo $object->contactno; ?></td>
<td><?php echo $object->username; ?></td>
<td><?php echo $object->empid; ?></td>
<td><?php echo $object->department; ?></td>
<td><?php echo $object->designation; ?></td>
<td><?php
echo form_open('welcome/deleteuser');
echo $object->empid;
$empid=$object->empid;
$department=$object->department;
echo form_hidden('empid',$empid);
echo form_hidden('department',$department);
echo "<p>";
echo form_submit('get_user','Delete');
echo "</p>";
echo form_close();
?></td>
</tr>
<?php
}?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong>
seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter
Version
<strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>
</body>
</html>
我想在我的网站上维护会话,直到用户选择退出(销毁它)。 提前谢谢。
答案 0 :(得分:3)
您需要使用$this->session->userdata('is_logged_in')
获取会话值
答案 1 :(得分:2)
您应该更改此行,检查会话值
if($this->session->all_userdata('is_logged_in')==1){
$this->load->view('welcome_message');
}
作为
if($this->session->userdata('is_logged_in')==1)
{
$this->load->view('welcome_message');
}
您可以更好地使用===运算符
//as your session value is string
if($this->session->userdata('is_logged_in')=== '1')
{
$this->load->view('welcome_message');
}