Jump to content

Recommended Posts

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!

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'); ?>

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!

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.