阻止访问 Codeigniter 中的函数
Block access to functions in Code Igniter
我正在 Code Igniter 中开发一个应用程序,遇到了一个问题:
我有几个访问数据库的函数,路由如下:
controller/function/variable
employess/deleteEmployee/4
所以,任何将此放在 url 上的人都会删除该员工。
我如何设法只允许已登录的管理员用户访问此功能?
有没有一种简单且被广泛接受的方法?
每次都要检查是否有用户登录,这个用户是否有权限?
此致,
这里是URL供参考...
https://ellislab.com/codeigniter/user-guide/general/hooks.html
这是一个简单的例子
Copy this piece of code as checksession.php and save it in /application/hooks
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Checksession
{
private $CI;
public function __construct()
{
$this->CI =&get_instance();
}
public function index()
{
if($this->CI->router->fetch_class() != "login"){
// session check logic here...change this accordingly
if($this->CI->session->userdata['userid'] == '' ){
redirect('login');
}
}
}
}
将此代码复制到 /application/config/hooks。php
$hook['post_controller_constructor'] = array(
'class' => 'checksession',
'function' => 'index',
'filename' => 'checksession.php',
'filepath' => 'hooks'
);
在 /application/config/config 中启用挂钩。php
$config['enable_hooks'] = TRUE;
希望对您有所帮助。
祝你好运,编码愉快
我正在 Code Igniter 中开发一个应用程序,遇到了一个问题:
我有几个访问数据库的函数,路由如下:
controller/function/variable employess/deleteEmployee/4
所以,任何将此放在 url 上的人都会删除该员工。
我如何设法只允许已登录的管理员用户访问此功能? 有没有一种简单且被广泛接受的方法? 每次都要检查是否有用户登录,这个用户是否有权限?
此致,
这里是URL供参考...
https://ellislab.com/codeigniter/user-guide/general/hooks.html
这是一个简单的例子
Copy this piece of code as checksession.php and save it in /application/hooks
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Checksession
{
private $CI;
public function __construct()
{
$this->CI =&get_instance();
}
public function index()
{
if($this->CI->router->fetch_class() != "login"){
// session check logic here...change this accordingly
if($this->CI->session->userdata['userid'] == '' ){
redirect('login');
}
}
}
}
将此代码复制到 /application/config/hooks。php
$hook['post_controller_constructor'] = array(
'class' => 'checksession',
'function' => 'index',
'filename' => 'checksession.php',
'filepath' => 'hooks'
);
在 /application/config/config 中启用挂钩。php
$config['enable_hooks'] = TRUE;
希望对您有所帮助。
祝你好运,编码愉快