dongmi1221 2014-06-12 11:18
浏览 31
已采纳

避免使用硬编码的凭据mysqli php?

I am developing a simple php application which needs connecting to Database This is part of the code that creates connection to db

private $mysqli;

public function __construct()
{
     $this->mysqli = new \mysqli("localhost", "root", "password", "mydb");
}

My question: how to avoid hard-coded credentials, The client required me to avoid this?

where should I put these setting?

  • 写回答

3条回答 默认 最新

  • douyuefei3546 2014-06-12 11:22
    关注

    Use config file:

    require_once("../path-to-your-config/config.php");
    

    Example usage:

    require_once("../path-to-your-config/config.php");
    
    class ABC {
    
        private $mysqli;
    
        public function __construct()
        {
             $this->mysqli = new \mysqli(Conf::HOST, Conf::USER, Conf::PASSWORD, Conf::DB);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?