2011-12-15 67 views
0

我使用Codeigniter来构建我的项目。PHP-Codeigniter中的构造函数

这里我有一些疑惑或需要一些澄清。

我可以使用构造函数做一些影响codeigniter/php中所有其他函数的事情吗?

请看看这里:

<?php 
class New extends CI_Controller 
{ 
function __construct() 

{ 

//Constructor codes... 

} 
function Create_page() //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function Edit_page()  //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function Delete_page() //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function about_us() //This is a public action no need to Log in 
{ 
    // this is a pulic action ,no need to check the login status 
} 
} 
?> 

正如你可以看到,我需要检查每私有函数状态记录下来,

有什么办法,我可以做到这一点的构造?使构造将审核登录或没有....但只需要影响某些功能......

+0

有所有公共职能 – ajreal 2011-12-15 13:03:34

+0

@ajreal这是合乎逻辑的。 – Red 2011-12-15 13:13:42

回答

2

呼叫在构造函数,检查步骤如下:

  • 定义哪些方法需要登录,哪些没有。
  • 检测当前的方法调用。
  • 如果需要,然后做登录,否则不。

<?php 
class New extends CI_Controller 
{ 

    var $publicMethods = array("aboutUs"); 

function __construct() 

{ 

//Constructor codes... 
    $this->_validateLogin(); 

} 

function _validateLogin() 
{ 
    $router = &load_class('Router'); 
    $currentMethod = $router->fetch_method(); 
    if(in_array($currentMethod,$this->publicMethods) == false){ 
     // call some login functionality 
    } 
    } 

function Create_page() //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function Edit_page()  //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function Delete_page() //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function about_us() //This is a public action no need to Log in 
{ 
    // this is a pulic action ,no need to check the login status 
} 
} 
?> 
0

如果你给你的构造函数的用户名和密码,比检查它是否在你面包车将它保存在loggend一个var(真/假)

class newclass{ 

public function __construct($username,$password){ 

    $query = mysql_query("SELECT * FROM accounts WHERE user = '".$username."' AND pass = '".$password."'"); 
    if(mysql_num_rows($query) > 0){ // if username and password match in database your loggend in 
    $this->loginCheck = true; 
    } 
    else{ // if not match not logged in 
    $this->loginCheck = false; 
    } 
}//end constructor 

public function another(){ 
    if($this->loginCheck){ 
    return "logged in"; 
    } 
    else{ 
    return "nog logged in"; 
    } 

}// end another function 

}//end class 

$class = new newclass($user,$pass); 
echo $class->another(); 
0

要回答你的问题,是的,你可以做。创建库并添加一个函数,如下所示,并在构造函数中加载库并调用该函数。我假设你设置一个信号,当用户登录到你的网站,像会话中的user_login。希望这回答你的问题。

// Place this function in the user library 
    check_user_login(){ 
    $obj = get_instance(); 
    if($obj->session->userdata('user_logged') != true): 
    // redirect to login page 
    endif; 
}  
<?php 
class New extends CI_Controller 
{ 
function __construct() 

{ 

//Constructor codes... 
//load the library, 
$this->load->library("library_name"); 
// call the function name 
check_user_login(); 

} 

function Create_page() //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function Edit_page()  //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function Delete_page() //The user need to be Logged in to perform this 
{ 
//Checking whether the user logged in or not if yes allowed else denied. 
} 
function about_us() //This is a public action no need to Log in 
{ 
    // this is a pulic action ,no need to check the login status 
} 
} 
?> 
0

基本上,你应该分开要由logined用户和每个人都可以访问的公共方法使用这些私有方法。如果你真的想这样做,你可以按照下面的方法来做。此外,您可以修改auth_method()以通过更多身份验证来满足您的需求。

class Page extends CI_Controller { 

public function __construct() 
{ 
    parent::__construct(); 
    $this->auth_method(); 
} 

public function create_page() 
{ 
    // ... 
} 

public function edit_page() 
{ 
    // ... 
} 

public function delete_page() 
{ 
    // ... 
} 

public function about_us() 
{ 
    echo 'About Us Page'; 
} 

private function auth_method() 
{ 
    $protected_methods = array('create_page', 'edit_page', 'delete_page'); 
    $segs = $this->uri->segment_array(); 
    $method = isset($segs[1]) ? trim($segs[1]) : 'index'; 
    if(in_array($method, $protected_methods)) 
     exit("Access Denied."); 
} 

} 
//END CLASS 
2

我通过以下方式解决了这个问题:

  1. 我做了所谓的“MY_Controller”延伸CI控制器类的新核心类。
  2. 我写了一个doAuth()验证用户或将用户重定向到登录控制器的方法。
  3. 现在我呼吁所有方法巫婆此方法必须是安全的

这似乎是一个很多比其他aproaches不太实用,但如果你决定你需要另一个控制器功能验证可以节省您的一些工作。