Solved

how to secure my credintials while connecting using pdo

Posted on 2007-11-22
6
258 Views
Last Modified: 2008-05-02
hi all,

this is  a snippet of pdo database example using mysql I got from a book

attached as snippet to this message

and this is a paragraph of the same book:

In the database connection examples we just saw, I included my access credentials
within the DSN, or in the $user and $pass variables, but I did so for illustration
purposes only. This is not standard—or appropriate—practice, since this inform­
ation can by misused by malicious parties to access your database.

now my question is how can I make that example secured as much as I can?
what is the most secure way to pass my credintials to the dsn?

can you please send me an example to illustrate the details or point me to a link that do that ?

thanks in advance
best regards
HG

<?php

$dsn = 'mysql:host=localhost;dbname=world;';

$user = 'user';

$password = 'secret';

try

{

  $dbh = new PDO($dsn, $user, $password);

}

catch (PDOException $e)

{

  echo 'Connection failed: ' . $e->getMessage();

}

?>

Open in new window

0
Comment
Question by:shang3000
  • 3
  • 3
6 Comments
 

Author Comment

by:shang3000
ID: 20334137
sorry for typos here is the pragraph in readable format:
In the database connection examples we just saw, I included my access credentials
within the DSN, or in the $user and $pass variables, but I did so for illustration
purposes only. This is not standard or appropriate practice since this information can be  misused by malicious parties to access your database.
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 20342281
There are several ways.

Some people prefer having the mysql user and mysql pwd in your php environment by setting them in vhost conf or similar places. Others just put the user and passwd in a file that is only accessible to your webserver user and is outside of the webroot.
A third option is to have your end user type in the actual username/password. This is a situation only appropriate to tools with few users.

-r-
0
 

Author Comment

by:shang3000
ID: 20342352
hi Roonaan,
thanks for reply,
can you please give me more details about each method
if there is any links, articles, books that would be great
best regards
HG
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Expert Comment

by:Roonaan
ID: 20342738
The second method is just putting a credentials.php somewhere outside your webroot. Then include it like: require '/path/to/config/outside/webroot/credentials.php';

The third is by having a authentication form, where the username and password are validated by seeing if you are able to connect to your database using those credentials.

The first mode I have personally not installed. If i recall correctly, you can use apache configuration files to set some "constants", which can be read from php using getenv.

0
 

Author Comment

by:shang3000
ID: 20342764
hi roonaan,
thanks again for reply,

1.
>>>The second method is just putting a credentials.php somewhere outside your webroot. Then
>>>include it like: require '/path/to/config/outside/webroot/credentials.php';
I think I'm getting this one
does this code work fine (please correct me if I'm wrong)
 require (/../pwd.php);

2.
>>>The third is by having a authentication form, where the username and password are validated
>>>by seeing if you are able to connect to your database using those credentials.
this one Is not clear for me would you please give me an example to distinguish the difference between this method and the others

3.
>>>The first mode I have personally not installed. If i recall correctly, you can use apache
>>> configuration files to set some "constants", which can be read from php using getenv.
can you guid me where to get a sample of code for that I didn't use this metho befor

4. what about encryption/decryption  and how can work with it in that issue

please help me in those points and that would be greate and if you can guide me to links or articles or books that talk about that  I'd be thankful for you

best regards
HG
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 20342823
The second method will not store the mysql login anywhere, but realy on your end-user to input correct mysql login credentials. You then use the credentials entered by the end user for your mysql connection.                

Encryption/decryption would make sense if you wouldn't have to enter the encryption key inside your php script. If you can encrypt your php itself using ioncube or similar tools, then that would be an option.

You can find quite some articles on mysql/php security from google.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now