Solved

how to secure my credintials while connecting using pdo

Posted on 2007-11-22
6
257 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

759 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

23 Experts available now in Live!

Get 1:1 Help Now