[英]Constructors in PHP-Codeigniter
我正在使用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
}
}
?>
如您所見,我需要檢查每個Private功能上的Loged in狀態,
有什么辦法可以在構造函數上執行此操作嗎? 這樣構造函數將檢查是否已登錄....但是只需要影響某些功能...
在構造函數中調用一個函數,該函數檢查以下步驟:
<?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
}
}
?>
我通過以下方式解決了這個問題:
doAuth()
方法,該方法對用戶進行身份驗證或將其重新路由到登錄控制器。 它似乎比其他方法實用性差很多,但是如果您決定需要其他控制器來進行身份驗證,則可以節省一些工作。
如果您為構造函數提供密碼和用戶名,然后檢查是否將其日志記錄保存在var(true / false)中
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();
要回答您的問題,可以的。 創建庫並添加一個函數,就像我在下面所做的那樣,然后在構造函數中加載該庫並調用該函數。 我假設您在會話中用戶登錄到您的站點(如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
}
}
?>
基本上,您應該將那些要由登錄用戶使用的私有方法與可以由所有人訪問的公共方法分開。 而且,如果您確實想這樣做,則可以執行以下方法。 此外,您可以修改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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.