fmitchell Posted November 12, 2009 Share Posted November 12, 2009 Currently the tool that I am working on has it's database connection info hardcoded into several of the PHP files (every file that contains code that needs to connect to the database). For example, in one file I have this (starting around line 39): $dbHost = "localhost"; $dbUser = "ftl-lab-dba"; $dbPass = "ftlftl"; $dbDatabase = "ftl-lab"; $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") or die ("Error connecting to database."); This makes it a real pain to switch from one database to another and move the tool around to different development servers. Someone mentioned to me that a better way would be for these scripts to read the database information from a configuration file. I found some general config files that look like this: DB_HOST=localhost DB_PORT=3306 DB_TYPE=mysql DB_NAME=ftl-lab DB_USERNAME=ftl-lab-dba DB_PASSWORD=ftlftl The problem I am having is that I need to write a function that reads data from a configuration file rather than hardcoding the database info into each PHP script. You can assume that the configuration file will be named budget_tool.cfg, that it will live in the same directory that the PHP files live in, and that it has the format shown above. I've been digging around online but can't seem to find anything that will work like this. I figure there's no harm in asking here. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/181301-using-php-to-link-to-a-mysql-config-file/ Share on other sites More sharing options...
Schlo_50 Posted November 12, 2009 Share Posted November 12, 2009 Might have missed the point completely here but you should be able to just put your database connection script in a file called 'connection.php' for example and then include that file in your other scripts where necessary. <?php include('connection.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/181301-using-php-to-link-to-a-mysql-config-file/#findComment-956400 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 hard coding it in a .cfg file and hard coding it in a .php are not much different. but if you must look into file_get_contents(), fread(), feof() and many other of the php file handling functions Quote Link to comment https://forums.phpfreaks.com/topic/181301-using-php-to-link-to-a-mysql-config-file/#findComment-956407 Share on other sites More sharing options...
Schlo_50 Posted November 12, 2009 Share Posted November 12, 2009 hard coding it in a .cfg file and hard coding it in a .php are not much different. Indeed. Quote Link to comment https://forums.phpfreaks.com/topic/181301-using-php-to-link-to-a-mysql-config-file/#findComment-956413 Share on other sites More sharing options...
fmitchell Posted November 12, 2009 Author Share Posted November 12, 2009 The problem with just doing: <?php include "budget_tool.cfg"; $db = mysql_connect("$dbHost", "$dbUser", "$dbPass") ......... is that budget_tool.cfg would have to be in php syntax and anyone who edits that file has a direct way to inject stuff into my code. I need to be able to use a plain text file that looks something like this: DB_HOST=localhost DB_PORT=3306 DB_TYPE=mysql DB_NAME=ftl-lab DB_USERNAME=ftl-lab-dba DB_PASSWORD=ftlftl and somehow read it and then match those variables up with the connection: $dbHost = "DB_HOST"; $dbUser = "DB_USERNAME"; $dbPass = "DB_PASSWORD"; $dbDatabase = "DB_NAME"; and so on... Thanks for the help though, under different circumstances I would completely aggree with using a php include and another php file with my connection info in php syntax. If you have any ideas on how to do this usingPHP file I/O let me know. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/181301-using-php-to-link-to-a-mysql-config-file/#findComment-956458 Share on other sites More sharing options...
mikesta707 Posted November 12, 2009 Share Posted November 12, 2009 well if someone has access to your .cfg file, than they have access to the rest of your files.. no? but look into feof() (or file_get_contents), and try to get the contents of each line of the .txt file, and then come back with whatever issues arise Quote Link to comment https://forums.phpfreaks.com/topic/181301-using-php-to-link-to-a-mysql-config-file/#findComment-956468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.