Jump to content

Encrypting database username/password..


cgm225

Recommended Posts

When using the following code to connect to a databse::

$DB_host = "localhost";
$DB_user = "my_user_name";
$DB_password = "my_password";	
$DB_name = "my_database";

mysql_connect($DB_host,$DB_user,$DB_password) or die("Couldn't connect to the database.");
mysql_select_db($DB_name) or die("Couldn't select the database");

 

Is there anyway to keep the username and password encrypted in the actual php script, like with MD5, or something like that?  I ask, because I would like it to be hidden if I have someone else review my code.

 

Thank you all in advance!

Link to comment
Share on other sites

The way i get around that is to use an include file on any php script needing database access.

 

Keep your connection string as it is and copy it over to another file, and name that file global.php or something similar.

 

At the top of everypage (however, underneath if you are using sessions or anything else header related) call in the connection string using

 

include_once ("global.php");

 

And your database will connect, leaving you free to give the script to whomever you wish.

 

I tend to keep my included scripts in an include folder, in which case I am including /include/global.php

 

Hope this helps and is what you were looking for

:)

Nick

Link to comment
Share on other sites

Here is the way I do it.

 

In an included file create a function called connectdb() (or whatever you wanna call it.)

 

include.php

<?php
///////////////////////////////
//         connectdb         //
///////////////////////////////
function connectdb()
{
mysql_connect('HOST', 'USERNAME', 'PASSWORD');
mysql_select_db('DATABASENAME') or die('Unable to select local database!');
}
?>

 

This include file is included in all my pages by way of the template structure I have set up. Then anytime I need to initiate a connection, I simply call

connectdb();

 

This way I can make a connection easily and it is separate from the rest of the code.

 

       

<?php
        connectdb();
$query="INSERT INTO tests (testname,questions)VALUES('$testname','$number_questions')";
$result=mysql_query($query);
?>

 

Link to comment
Share on other sites

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.