Forum Moderators: coopster

Message Too Old, No Replies

Secure mySQL login w/PHP

Best way to encrypt the login/password to connect to mySQL database

         

stylez

11:44 pm on Apr 29, 2005 (gmt 0)

10+ Year Member



Hi,

I've worked with PHP abit and just recently I've done some mySQL work for classes and I have a better feel for it now than before. I'm thinking of redoing my db that used to be on a flat file to mySQL. I've worked with mySQL syntax and some php, but nothing with each other. I looked up simple tutorials that showed you how to connect to mySQL db with small functions/etc. Here is snippet of something simple:

<?php
mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
echo "Connected to MySQL<br />";
?>

This database that I'm writing will have a limited priv. that is only acquired through localhost and no ext. connections. I wanted to someone have it read from a file with encrypted login/pass. I know there are some md5 encryption out there and I already created the conversion of the login/pass to md5 hash. I'm not sure if that is the best way to do it. Both our workers and students will be able to access the diff. php db pages and the pages must store the login/pass. I was hoping someone could direct me on the best way to implement this workorder database with php and mySQL and any security hints or snippets of code or tutorial would help me. Only recently have I worked with PHP and mySQL. I'm still new in both areas, but I'm more familiar with it now than few months before. Thank you for your time.

leadegroot

12:40 pm on Apr 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not entirely sure I am understanding your question, but this is what I do:

include '/share/dir/dir/dbData.incl.php';
mysql_connect($nmHost, $nmUser, $txPassword) or die(mysql_error());

(note i havent checked the order of params there)
where dbData.incl.php is something like:

$nmHost = "localhost";
$nmUser = "admin";
$txPassword = '1admin';

I hope this makes sense - basically, remove the data from the publically readable area of the webserver.
The file still needs to be world-readable for the webserver to be able to read it (or you could get extra clever and make the file's group the same as the webserver) but it is placed in a directory that cannot be viewed from the web.

HIH!

Stormfx

4:43 pm on Apr 30, 2005 (gmt 0)

10+ Year Member



Do what ledge said, store another file above the web root with the username/password informationfor accessing the database and then include() it. Then it won't be readable via a web browser. I do something to the effect of:

dbcfg.php
---------

$db = array(
'hostname' => 'localhost',
'username' => 'testuser',
'password' => 'blahblah',
'database' => 'mydata',
);

functions.php
-------------

function dbconn() {
if(!include_once('/path/to/dbcfg.php')) {
die('Error include file...');
}

if (!$link = mysql_connect($db['hostname'],$db['username'],['password'])) {
die('Error connecting...');
}

if (!mysql_select_db($db)) {
die('Error selecting...');
}

return $link;
}

file.php
--------

include('/path/to/functions.php');

$link = dbconn();

That's a very simplified version, but it shows the idea. You want the db config outside of the web root and then optionally, include the file via a function like above to keep the config's variable scope unavailable outside of the function.

For encrypting other username/password for user accounts, etc. An MD5 comparison on the password is sufficient.

stylez

6:40 pm on Apr 30, 2005 (gmt 0)

10+ Year Member



Thank you for these great suggestions. After few hours searching the web, many sites gave similiar examples of an include file from another dir that is linked to the main php file. If I was to store the user/pass as MD5 encrypted hashes, what functions would I need so that

$db = array(
'hostname' => 'localhost',
'username' => 'testuser',
'password' => 'blahblah',
'database' => 'mydata',
);

Could be written as :

$db = array(
'hostname' => 'md5 hash',
'username' => 'md5 hash',
'password' => 'md5 hash',
'database' => 'md5 hash',
);

Also after the admin account has been setup to check the system. I wanted to create a user profile setup where I add users and I want their information hashed. Is there a simple command that allows the values to be hashed and sent to the mySQL db. Thanks for your time once again.

Stormfx

10:05 pm on Apr 30, 2005 (gmt 0)

10+ Year Member



Not totally for sure what you're trying to achieve, but you can just put the md5 hashes in instead of the various settings. As for hashing the users' information, that depends on if you want to be able to read it later. If not, just md5() it. Otherwise, you'll have to use some sort of encrypt function prior to inserting in the database. Also, I do believe MySQL has built-in encrpytion functions.

stylez

10:36 pm on Apr 30, 2005 (gmt 0)

10+ Year Member



Well I wasn't sure which is the best to go at it. Currently I have two files.

Login.php -> Calls connect.php that knows how to access the database to check if the registered user is in the system
Connect.php -> has the code that actually connects to the db to check for a connection.

For security reason, I know you don't want to put the connection code with the login/pass open to the public. So I wanted to have Connect.php be an included function in Login.php, for more security, I wanted to know if I had an md5 hash of the login/pass in Connect.php. Would Login.php be able to translate it and connect to the database. I just didn't want the generic mySQL connection be seen publicly, if someone had access to the server. Thank you for your time.

Stormfx

5:04 am on May 1, 2005 (gmt 0)

10+ Year Member



Primary Database Connection:

If you have FTP access one level above your web root directory, then simply store the database connection info there, unencrypted. Then it's not readable from a web browser.

If it's not stored outside of the web root, no amount of encryption or hashing will protect it unless you specifically deny web access to that type of file via .htaccess or URLScan for IIS. Then you can still include it but it will not be served via the web server. And of course for that method, you wouldn't need to hash/encrypt it.

User Accounts:

When you create/add/whatever a user account, store their username, but md5() the password before storing it. Then, when you need to compare it, md5() the submitted password before the comparison. This will automatically make it case sensitive, by the way.

Hope that helps :)