Results 1 to 3 of 3

Thread: PHP - Check and Test config file

  1. #1

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    PHP - Check and Test config file

    I just started the "final" version of a PHP framework used to drive multi-lingual sites. Very useful, and hope you guys can help me check the code as I go along (thanks to me having to code in various languages, I'm not an expert in any)
    The following class will be included in all pages, and used to check a configuration file is in place, and if so, the values are correct (for now, just for database connection).
    Note:
    $level is a variable I create in each page. If the file in the root, it will be empty, if one level down, it will be "../". I don't know how else to correctly specify the path to another file.
    For what it matter, I include a sample project (MySampleSite) and the framework (DataShop). For now it does nothing, I just let it start up and check the configuration file is present and ok.

    Code:
    <?php
    
    	class base {
    		
    		var $error;
    		var $level;
    		var $connection;
    		
    		function __construct($level)  {
    			// we will be working with session variables soon, so make sure session have been started
    			if( !isset( $_SESSION ) ) { session_start(); }
    			$this->level = $level;
    			//do we have a configuration file with valid settings?
    			$this->error = $this->validate_configuration_file();			
    		}
    		
    		/* Ensure the configuration file exists, and all settings set and valid */
    		function validate_configuration_file() {
    			// Check to see if the configuration file is present
    			if(!file_exists($this->level.'_scripts/ds-config.php'))
    				return 'The configuration file does not exists';
    			// Include configuration file, so we can check it's values
    			require_once($this->level.'_scripts/ds-config.php');
    			// Ensure the required Constants have been defined
    			if (!defined('DB_HOST'))
    				return 'Database Host is not defined in ds-config.php';
    			if (!defined('DB_NAME'))
    				return 'Database is not defined in ds-config.php';
    			if (!defined('DB_USER'))
    				return 'Database Username is not defined in ds-config.php';
    			if (!defined('DB_PASSWORD'))
    				return 'Database Password is not defined in ds-config.php';	
    			// Ensure a connection can be made to the database server using these values;
    			@ $this->connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    			if (mysqli_connect_errno())
    				return "Could not connect to database. Error No:".mysqli_connect_errno()."<br />";
    			$this->connection->query("SET NAMES 'utf8'");
    		}
    		
    		/* Check to see if we have a valid logged in user		
    			TO DO: ADD CODE TO VALIDATE LOGGED IN USER AGAINST DATABASE
    		*/
    		function is_logged_in() {
    			return(isset($_SESSION['uid']) && $_SESSION['uid'] !='');
    		}
    	
    	}
    ?>

    Hope I can make this a success with your help.

    Basically, DataShop is a PHP framework to drive multi-lingual websites. The aim is to have a system you can add to a new or existing website, and once implemented, the site owner can add languages, have translations done and then activate the new language without any help of the developer.
    It is not a perfect system, but works well (I used the older versions with a few sites, and no complaints) for sites where each language version is the same, and you don't want to use separate pages for each language (just too hard in a one-man development team).
    Attached Files Attached Files
    Last edited by StrangerInBeijing; May 19th, 2009 at 11:02 AM.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  2. #2

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: PHP - Check and Test config file

    I removed the check to see if the Constants have been defined.
    Database connection will fail anyhow if they are not.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  3. #3

    Thread Starter
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: PHP - Check and Test config file

    Added new version with login functionality.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width