我在Codeigniter中有这个View页面
<?php
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
return false;
}
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" type="text/css" media="screen" />
</head>
<body>
<p><?php echo form_open('it/create'); ?></p>
<p><?php echo form_input('department', 'department'); ?></p>
<p><?php echo form_textarea('problem', 'Issue'); ?></p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<p><?php echo form_close(); ?></p>
</body>
</html>
这是控件
class Create extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->is_logged_in();
}
public function create_issue()
{
$this->load->view('v_it');
}
public function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
echo "you don't have permission";
}
}
}
但是当我直接从url登录到视图时,它打开没有问题,这有什么不对,我甚至在视图页面中直接添加了SESSION,就像我添加了它一样,但它打开了。
更新:这就是我在另一个控制器中添加SESSION的地方:
$data = array(
'username' => $this->input->post('username'),
'is_loged_in' => 'true' );
$this->session->set_userdata($data);
redirect('create/create_issue');
答案 0 :(得分:1)
public function create_issue()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
echo "you don't have permission";
} else {
$this->load->view('v_it');
}
}
在你的控制器中。不确定你认为返回false应该在视图中做什么,但它不会退出任何它只是返回false到无处,然后继续显示HTML。你确实需要控制器本身的这种逻辑,你只需检查登录,如果它返回false,则根本不加载视图。
答案 1 :(得分:1)
你应该写入控制器的授权逻辑对你有帮助。尽管你需要编写逻辑来查看参考文件
使用控制器:
public function create_issue()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if (is_logged_in )
{
redirect();//redirect your login page to the user
} else {
$this->load->view('v_it');
}
}
Wthin view logic:
<?php
$is_logged_in = $this->session->userdata('is_logged_in');
if (!isset($is_logged_in) || $is_logged_in != true)
{
echo "you are not valid user";
}
else{
?>
<html>
<head>
<title></title>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" type="text/css" media="screen" />
</head>
<body>
<p><?php echo form_open('it/create'); ?></p>
<p><?php echo form_input('department', 'department'); ?></p>
<p><?php echo form_textarea('problem', 'Issue'); ?></p>
<p><?php echo form_submit('submit', 'Submit'); ?></p>
<p><?php echo form_close(); ?></p>
</body>
</html>
<?php } ?>