Welcome! Log In Create A New Profile

Advanced

Config.php

Posted by 77959132 NeoGek 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Config.php
April 06, 2013 10:01AM
Hi Guys,
Stupid question but what is the best way to code the config.php file? Done some googling and there are various ways that I've come across:

Language: PHP
<?php // Connection';s Parameters $db_host="localhost"; $db_name="database_name"; $username="database_username"; $password="database_password"; $db_con=mysql_connect($db_host,$username,$password); $connection_string=mysql_select_db($db_name); // Connection mysql_connect($db_host,$username,$password); mysql_select_db($db_name); ?>

Language: PHP
<?php $conf[';db_hostname';] = "localhost"; $conf[';db_username';] = "username"; $conf[';db_password';] = "password"; $conf[';db_name';] = "sample"; ?>

Language: PHP
<?php // MySQL connect information. $c_username = ""; // database username $c_password = ""; // database password $c_host = ""; // database host $c_database = ""; // database name   // connecting to the database $connection = mysql_connect($c_host, $c_username, $c_password) or die ("It seems this site';s database isn';t responding.");   mysql_select_db($c_database) or die ("It seems this site';s database isn';t responding."); ?>

Think the last one looks good but just wanna be sure
avatar Re: Config.php
April 06, 2013 12:57PM
It really does not make a difference as long as it is value and you are comfortable with it.

____________________________________________Nazi Coder____________________________________________

I'm not antisocial, I'm just not user friendly

"It's not a bug; it's an undocumented feature!" ~ some unknown Microsoft developer
Re: Config.php
April 08, 2013 05:37AM
Cool thanks, I'm gonna use the 3rd option as I like how it's got the "or die" statements in there for checking
Re: Config.php
April 08, 2013 08:42AM
I use the third option.

how will the 2nd option connect? Can I see the rest of it please.
Re: Config.php
April 09, 2013 05:28PM
I too am using the 3rd option.

I found the 2nd option on Stackoverflow.com as the following query:


I have created a config file that looks like:
Language: PHP
$conf[';db_hostname';] = "localhost"; $conf[';db_username';] = "username"; $conf[';db_password';] = "password"; $conf[';db_name';] = "sample"; $conf[';db_type';] = "mysql"; $conf[';db_prefix';] = "exp";

and saved it as config.php.

Similarly the autoloader looks like:
Language: PHP
class autoloader { public static $loader;   public static function init() { if(self::$loader == NULL) { self::$loader = new self(); } return self::$loader; }   public function __construct() { spl_autoload_register(array(this, ';library';)); spl_autoload_register(array(this, ';controller';)); spl_autoload_register(array(this, ';helper';)); spl_autoload_register(array($this, ';model';)); }   public function library($class) { set_include_path(get_include_path() . PATH_SEPARATOR . ';/lib';); spl_autoload_extensions(';.php';); spl_autoload($class); }   public function controller($class) { set_include_path(get_include_path() . PATH_SEPARATOR . ';/controller';); spl_autoload_extensions(';.php';); spl_autoload($class); }   public function helper($class) { set_include_path(get_include_path() . PATH_SEPARATOR . ';/helper';); spl_autoload_extensions(';.php';); spl_autoload($class); }   public function model($class) { set_include_path(get_include_path() . PATH_SEPARATOR . ';/model';); spl_autoload_extensions(';.php';); spl_autoload($class); } }

  • Where should I place both these files? In Root folder?
  • How these $configs will be available across the application?
  • How whould I use them in index.php? Should I create for $config array also a class?
  • How may I deny direct access to config.php file?

This was answered as follows:
You must include your config file. I would recommend using require_once. Add this code to any files that will need the config variables. The best way to do this is to use a controller file, usually an index.php. That way you only need to add the require_once to a single file.
Language: PHP
require_once("./config.php");
Don't worry about people viewing your database password, all php variables are purely server-side, and no php code or variables can be viewed by a client unless explicitly echoed out.

Hope this helps
Re: Config.php
April 15, 2013 11:43PM
I think the 2nd 1 is simpler even if they are all the same.

78009855
avatar Re: Config.php
April 24, 2013 06:37AM
Hi there everyone,

Could we just define constants in our config.php and then use the functions.php to connect to the database?

Cheers
avatar
Mac
Re: Config.php
April 24, 2013 08:50AM
You can apply whatever you have learned....
Re: Config.php
April 24, 2013 11:57AM
Wow, you guys know a lot more than me.

I just did the following:-

Language: PHP
<?php   $user_name = "root"; $pass_word = ""; $database = "registration"; $server = "127.0.0.1";   $db_handle=mysql_connect($server,$user_name,$pass_word); $db_found=mysql_select_db($database,$db_handle);   ?>

Seems to work.
Re: Config.php
April 24, 2013 10:34PM
me to I think the 2nd one is simpler guyz.

78009855
Sorry, only registered users may post in this forum.

Click here to login