vpetkovic
10/15/2019 - 7:04 PM

DB instance

<?php 

    class config
    {
        private static $instance;
        
        public function __construct()
        {
            self::$instance = $this->dbConnect();
        }
        
        public function appSettings() {
            // Enter the site name
            $_SITENAME_ = '[Site Name]';
    
            // Database Server
            $_SRV_ = '[IP]';
    
            // Choose desired environment. 'Prod' or 'Dev'
            $_DB_ = '[DB]';
            
            return array(
                "AppName" => $_SITENAME_,
                "Server" => $_SRV_,
                "DB" => $_DB_
            );
        }
        
        public function dbConnect() {
            $app = $this->appSettings();
    
            $serverName = $app['Server']; //serverName\instanceName
            $UID = array( "Database"=>$app['DB'], "UID"=>"[user]", "PWD"=>"[pass]"); // For use with username and password not win authentication
    
            // The connection will be attempted using Windows Authentication.
            $connectionInfo = array( "Database"=>$app['DB']);
           return sqlsrv_connect( $app['Server'], $connectionInfo);
        }
        
        public function user() {
            $username = getenv("LOGON_USER");
            $username = explode("\\", $username);
            $username = $username[1];
            
            return $username;
        }
        
        
        
        
        
        
        // Getting already active db instance
        public function getInstance() {
            if (empty(self::$instance)) {
                self::$instance = $this->dbConnect();
            }
        
            return self::$instance;
        }
        
    }