如何在代码点火器控制器中包含PHP文件


how to include php file in codeigniter controller

我有一个登录控制器,它具有使用我的域凭据连接到LDAP的功能。

class Login extends MX_Controller {
const USER = "DOMAINACCOUNT";
const PASS = "DoM@inP@ssw0rd";
public function checkWW930() {
    $ldapserver = "ldap://ww930.sampledomain.net";
    $dn = 'dc=ww930,dc=sampledomain,dc=net';
    $ldapconn = ldap_connect($ldapserver) or die("Could not connect to $ldaphost");
    //ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
    //ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
    $user = self::USER;
    $pass = self::PASS;
    $bind = @ldap_bind($ldapconn, 'ww930'''.$user, $pass);
    $filter = "(samaccountname=". $this->input->post('username') .")";
    $result = ldap_search($ldapconn, $dn, $filter);
    $info = ldap_get_entries($ldapconn, $result);
    if($info["count"] > 0) {
        return TRUE;    // account exists in ww930 domain
    } else {
        return FALSE;   // account does not exist in ww930 domain
    }

这工作正常,但我想将我的凭据保存在一个单独的文件中,以便如果需要,我可以在其他控制器中使用它。我还想将其保存在一个文件中,以便在密码过期时,我只需要更新一个文件。我正在考虑将我的凭据放在凭据中.php文件,然后添加包含("凭据.php");有人可以帮我如何实施它吗?非常感谢。

您可以在/config/constants.php中设置常量

通过在文件末尾添加这两行

defined('USER')      OR define('USER', "DOMAINACCOUNT");
defined('PASS')      OR define('PASS', "DoM@inP@ssw0rd");

现在,您可以在项目中的任何位置使用常量。

你可以尝试这样的东西来抽象和组织你的结构

在应用程序/配置/文件夹中创建一个名为 LDAP 的新文件.php

并将以下代码放入其中

$config["ldap_default"] = array(
    "server"  =>  "ldap://ww930.sampledomain.net",
    "dn"  =>  "dc=ww930,dc=sampledomain,dc=net",
    "user" => "DOMAINACCOUNT",
    "password"  => "DoM@inP@ssw0rd",
);

创建一个名为 CustomLDAPConnection 的库.php(将其放在应用程序/库文件夹中)

class CustomLDAPConnection
{
    private $ci;
    private $arrConfig = array();
    public function __construct()
    {
        $this->ci = &get_instance();
        $this->ci->load->config("ldap");
    }
    public function checkWW930($userName = false, $configGroup = "default") 
    {
        if (!$userName) return false;
        $this->arrConfig = $this->ci->config->item("ldap_".$configGroup);
        $ldapconn = ldap_connect($this->arrConfig['server']) or die("Could not connect to $ldaphost");
        $bind = @ldap_bind($ldapconn, 'ww930'''.$this->arrConfig['user'], $arrConfig['password']);
        $filter = "(samaccountname=". $userName .")";
        //ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
        //ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
        $result = ldap_search($ldapconn, $this->arrConfig['dn'], $filter);
        $info = ldap_get_entries($ldapconn, $result);
        return ($info["count"] > 0) ?   true    :   false;
    }
}

然后在您的登录控制器中

class Login extends MX_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library("CustomLDAPConnection");
    }
    public function checkLDAPForUser()
    {
        return $this->CustomLDAPConnection->checkWW930($this->input->post("username"));
    }
}

它非常基本和简单,但它应该给你你需要的提示