Creating a configuration file for your web applications is very important.
This is where you store all your constant variables that you can use for your entire project.
There are a lot of ways that you can store these information:
1. Include a Php File that contains the variables
2. Store information to a database and retrieve
3. Create an ini file
Creating an ini file is my favorite way of storing my variables. :)
Here is how to use it:
1. Create the ini file and name it as sample.ini and put your variables on it like below:
;Note how comments are made with semi colons?
[db_info]
db_name = MyDatabase
rootdir = http://localhost/mywebpage/
2. Create a function that reads the ini file and include it on your web page.
Example:
function getIniValue( $name ){
$ini = parse_ini_file( "sample.ini" );
$name_var = $ini[$name];
return $name_var;
}
Note: You could also do a single call to parse_ini_file() function that returns an associative array of all the variables declared on your ini file if you need all of those variables on one page.
It depends on your approach on how you could optimize the processes on your page.
3. On your web page, whenever you need a constant value, call your function and assign it to a variable.
Example:
$dbname = getIniValue( "db_name" ); // Now $dbname = "MyDatabase"
One thing that we just need to remember when we use this is to restrict the access to this file because it can be opened and appear as plain text on your browser.
Another thing is, as much as posible, do not put any login information specially database constants like database host, root user and root password.
__________________________________________________
Web Programming Tutorial by Gerlie Mendoza | Web Developer
http://on.fb.me/Ay7OPZ
No comments:
Post a Comment