doumei1772 2014-09-16 16:13
浏览 63
已采纳

如何防止codeigniter中的某个特定视图不能通过url直接访问?

In view i have two folders pages, templates. Inside pages i have two folders nonmember and member. Inside templates i have header.php and footer.php. In nonmember folder i have a notification file, email_notification.php, along with other files like home.php, about.php etc.

pages are being generated dynamically using the following function

public function index(){
    $this->nonmember();
  }

public function nonmember($page = 'home'){ 

   if (! file_exists(APPPATH.'views/pages/nonmember/'.$page.'.php')) {
    show_404();
   }

   $data['title'] = ucfirst($page);

   $this->load->view('templates/header', $data);
   $this->load->view('pages/nonmember/'.$page, $data);
   $this->load->view('templates/footer', $data);

  }

And calling it in for showing login page or registration page

public function registration(){
//validation rules

   if($this->form_validation->run()){
    //add user to a temporary table
    //send an email with an activation code
    $this->nonmember('email_notification'); 
   //that view says click the link in email for activating account
     }else{
    $this->nonmember('registration');
    }
}

Problem is that email_notification view can also be accessed via url, which is not desirable. How do i prevent email_notification from direct access? like if user try to access it using url i want to redirect them back to home page, or show_error() ?

  • 写回答

3条回答 默认 最新

  • doulei6778 2014-09-16 18:31
    关注

    As James Lalor already mentioned about using session which is a good solution. What i have done is just before $this->nonmember('email_notification'); i'm creating a session using

    $this->session->set_userdata('email_notification' => 1);
    

    And in the nonmember function i'm just checking if that session is set to 1 or not.

    if ($page == 'email_notification' && $this->session->userdata('email_notification') != 1) {
         redirect($this->agent->referrer());
       }
    

    Later when user activate their account i'm destroying the session

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

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码