dsag14654 2012-11-10 11:55
浏览 41
已采纳

保护codeigniter视图

I have this View page in Codeigniter

<?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>

and this is the controlle

    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";
            }
        }
    }

but when I'm loging in to the view directly from url it opens without problem, What's wrong whith this, I even added the SESSION in the view page directly like I added but it opens.

UPDATE: And that's where I add the SESSION in another controller:

$data = array(
                'username' => $this->input->post('username'),
                'is_loged_in' => 'true' );

            $this->session->set_userdata($data);
            redirect('create/create_issue');
  • 写回答

2条回答 默认 最新

  • dongqindu8110 2012-11-10 14:13
    关注
    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');
            }
        }
    

    In your controller. Not sure what you think the return false was supposed to do in the view but it's not exiting anything it's just returning false to nowhere, then continuing to display the HTML. You do want this kind of logic in the controller itself, you simply check for logged in and if it returns false don't load the view at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大