View Full Version : Storing user name and password for MYSQL

09-10-2007, 03:04 AM
I think I seen it some where but cant find it again
Is there a way to store
that is used to access MYSQL from a PHP page
in a seperate file and have the PHP page get it from there when needed.

09-11-2007, 09:37 AM
Here's a good place to start. (http://www.php-mysql-tutorial.com/)

09-11-2007, 12:26 PM
you could store them in an xml file?

09-11-2007, 12:32 PM
The common way of doing it is storing them in a PHP file as variables, then including it.

09-12-2007, 05:15 AM
The common way of doing it is storing them in a PHP file as variables, then including it.

Can you give excample of how to do it

09-12-2007, 06:15 AM
You can learn how to include files in a PHP file here (http://www.phpfreaks.com/tutorials/131/0.php)


$dbuser = "root"; //Mention the database user who can access the database and perform the operations in your case.
$dbpassword = "somepassword";

09-12-2007, 06:34 AM
$dbuser = "root";

Ouch. Never ever ever use root to do logins or anything to do with databases when the pages are going to be viewed on the internet. If a hacker gets into your site, and finds out your root password through your page, consider your database gone.

If you created separate accounts in MySQL with different privileges, the most damage a hacker could do is delete all your accounts (That's if you set the privileges to allow to delete)

09-12-2007, 06:37 AM
I usually do it this way because I like to use a $_CONFIG array. However, if you only have primitive values to store it would be simpler to use defines:
define('DBUSER', 'root'); // Why the heck are you using the root account?
define('DBHOST', 'localhost');
define('DBPASSWORD', 'somepassword');
mysql_connect(DBHOST, DBUSER, DBPASSWORD);If you use global variables in a function, you have to worry about writing
global $dbhost, $dbuser, $dbpassword;all the time. Defines remove the necessity for this.

09-14-2007, 04:38 AM
Also, do not (is that clear enough?) DO NOT store your username / password in anything but a .php page (or a page which the server will parse as php if it is accessed.)

The reason for this is simple. A few years ago, there was a silly fad to simply use *.inc for an include file. As you can see, this is not secure (http://www.google.com/search?hl=en&client=opera&rls=en&hs=28h&q=filetype&#37;3Ainc+%22mysql_connect%22&btnG=Search)...

View a couple of these, and presto! you have the database username, password and table names...

be very, very careful where you store those variables.

09-14-2007, 08:02 PM
I was going to post a similar reply to boogyman's suggestion of xml. Very bad idea.
It would store it fine and be accessible, but it would not be secure at all.

Using .php (and variations) is the only way to go, since php is secure in itself, and you are using it anyway. You could, in theory, store it some other way, but the necessarily piece is that php can use the data, so might as well make that the only way to hack it. If you use two methods, it just doubles the odds against you, not to mention that if your PHP isn't secure, there's no real point to it anyway. (Generally php should be secure, unless you have a security flaw in a script.)

the trend for .inc is kinda weird, though in some cases can be a nice distinction.
Clearly, it's not secure, and a bad idea as such, with these exceptions:
1. Use it for nonsecure code (ie some function that doesn't really give anything away about your system). //shrug... probably not the best idea.
2. Configure your host to treat .inc as a php parsed extension, and it will become secure.
3. I like, in some cases, using .inc.php, since it does differentiate, but keeps it as .php.

10-05-2007, 09:53 PM
Sorry did get back sooner but had other problems
I did this in the php file to access the user, pass, db
but I have to put that in the same place (folder)as the file accessing it
is the a way I can put something.inc.php in a differant folder