2011-12-15 66 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不太實用,但如果你決定你需要另一個控制器功能驗證可以節省您的一些工作。