【问题标题】:Constructors in PHP-CodeigniterPHP-Codeigniter 中的构造函数
【发布时间】:2011-12-15 12:22:54
【问题描述】:

我正在使用 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
}
}
?> 

如您所见,我需要检查每个私人功能的登录状态,

有什么方法可以在构造函数上做到这一点?这样构造函数就会检查是否登录......但它只需要影响一些功能......

【问题讨论】:

  • 所有公共函数都有
  • 哦...什么是合乎逻辑的?公共=私人? (这就是你的意思是合乎逻辑的?)
  • 你不应该避免使用保留关键字(“New”)作为类名吗?
  • @landons 这只是一个例子,请不要介意类名。

标签: php function codeigniter login constructor


【解决方案1】:

在构造函数中调用一个函数,它会检查以下步骤:

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

<?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
}
}
?> 

【讨论】:

    【解决方案2】:

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

    1. 我创建了一个名为“MY_Controller”的新核心类,它扩展了 CI 控制器类。
    2. 我编写了一个doAuth() 方法来验证用户身份或将他重新路由到登录控制器。
    3. 现在我从所有必须安全的方法中调用此方法

    这似乎比其他方法实用得多,但如果您决定需要另一个控制器来进行身份验证,它可以为您节省一些工作。

    【讨论】:

    • 这是最实用的方法。事实上,您可以更进一步,让一个控制器继承自 CI_Controller 以检查身份验证,而另一个控制器继承自 CI_Controller 以进行 REQUIRE 身份验证。然后,您可以随时获得可用的身份验证数据,并且您可以创建所有都需要身份验证的控制器。
    【解决方案3】:

    要在一行中回答您的问题,是的,您可以这样做。像我在下面所做的那样创建库并添加一个函数,然后在构造函数中加载库并调用该函数。我假设您在用户登录到您的站点时设置了一个信号,例如会话中的 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
    }
    }
    ?> 
    

    【讨论】:

      【解决方案4】:

      如果你给你的构造函数密码和用户名,然后检查它的loggend是否在你的van中保存在一个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();
      

      【讨论】:

        【解决方案5】:

        基本上,您应该将希望由登录用户使用的私有方法和每个人都可以访问的公共方法分开。如果你真的想做,你可以做类似下面的方法。此外,您可以修改 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-08-13
          • 1970-01-01
          • 2023-04-07
          • 2014-05-06
          • 2010-12-15
          • 1970-01-01
          • 2012-06-30
          相关资源
          最近更新 更多