代码之家  ›  专栏  ›  技术社区  ›  Imnotapotato

如果用户登录时连接到其他数据库,如何在配置类中存储数据库凭据

  •  0
  • Imnotapotato  · 技术社区  · 8 年前

    我正在开发一个webapp,每当它打开时,用户没有登录-默认设置为连接到数据库: maindb ,每当用户登录时,用户应断开 曼德布 并连接到他自己的数据库。

    我尝试使用if语句检查是否已保存 customer_host 和 customer 在会话中,但PHP不允许我在类变量中执行此类操作。

    我的问题是:

    1. 如何创建这样的功能,以便配置变量可以根据会话是否存在来设置自己。(使用对静态变量的调用)

    2. 这种结构/体系结构对我来说是否合适(“良好实践”)?

      class Config {

      # Website main URL
      const WEB_URL           = 'https://potato.com/';
      
      # Username 
      const DB_USERNAME       = 'potato';
      
      # Password 
      const DB_PASSWORD       = 'disismyrealpassword';
      
      # Set the DB servrer 
      static $DB_HOST         = (Session::exists('customer_host')) ? Session::get('customer_host') : '127.0.0.1';
      
      # Set DB name 
      static $DB_NAME         = (Session::exists('customer')) ? Session::get('customer') : 'maindb';
      
      # Sessopm name
      static $session_name    = 'user'; 
      
      # token name
      static $token_name      = 'token'; 
      

    PS:我不是那个决定为每个客户创建不同数据库的人,我不能在我目前的职位上更改数据库,所以请……试着集中精力 这 问题是,为什么每个用户都有不同的数据库。

    这是我的数据库类的一部分:

    private $host;
    
    private $user;
    
    private $password;
    
    private $dbname;
    
    
    
    /*************/
    /*  METHODS  */
    /*************/
    
    
    /**
     *   Get Database instance
     *
     */
    public static function getInstance()
    {
        if (!isset(self::$instance)) 
            self::$instance = new Database();
    
        return self::$instance;
    }
    
    
    /**
     *   Default Constructor 
     *
     *  1. Instantiate Log class.
     *  2. Creates the parameter array.
     */
    private function __construct()
    {
        // Load defaults
        $this->loadDefaults();
    
        // INIT VARIABLES
        $this->parameters = array();                                                   
    
        // Connect with global vars.
        $this->Connect($this->host, $this->user, $this->password, $this->dbname);
    }
    
    
    /**
    *
    *  Method to load the object with the defaut settings
    *
    */
    private function loadDefaults() {
        $this->user     = Config::DB_USERNAME;
        $this->password = Config::DB_PASSWORD;
        $this->host     = Config::$DB_HOST;
        $this->dbname   = Config::$DB_NAME;
    }
    

    如果您还建议添加其他内容,请询问,我将进行编辑。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Sugumar Venkatesan    8 年前

    在配置类中

    注意:登录后,请重定向到其他页面

    class Config {
      static $DB_NAME_BEFORE_LOGIN         = 'maindb';
      static $DB_NAME_AFTER_LOGIN         = 'own';
      public static get_database(){
        if(logged_in){
          ///do you logic and return the database name
           self::$DB_NAME_AFTER_LOGIN;
        }else{
          //do your logic and return the database name
           self::$DB_NAME_BEFORE_LOGIN;
        }
      }
    }
    

    在你的职能范围内

    private function loadDefaults() {
        $this->user     = Config::DB_USERNAME;
        $this->password = Config::DB_PASSWORD;
        $this->host     = Config::$DB_HOST;
        $this->dbname   = Config::get_database();
    }
    
    推荐文章