Mysql php security

From Cncz
Revision as of 12:11, 19 December 2007 by Petervc (talk | contribs)
Jump to: navigation, search

Mysql Security

Mysql and php, password protection

An often used combination for a website is php with mysql as database backend. This implies that the web server must be able to connect to the database. For this it needs a mysql account and password. A typical (but unsafe) solution is to store the account name and password somewhere in a file in the document treee of the website which is then read by a php script that makes the mysql connection. This is not safe because this file has to be readable for the webserver. On a webserver which hosts multiple sites it implies that the file must be readable for the user www, in practice this often means readable for the world. This means that the file is readable for everyone who has an FNWI account. Apart from that there is the risk that the file can be read through the webserver.

There is a solution for this problem, instead of including the password in plain text in a file, one can encrypt the password with a website specific secret key. In the php code which makes the connection with the mysql server you replace the line which sets the password:
$admin-password = "plaintextpassword";
## EXTRA construction to protect the password
require_once( "" );
$admin_password = decrypt("ENCRYPTED PASSWORD");
where "ENCRYPTED PASSWORD" has to be replaced with the string you receive from C&CZ. Beware that in case this assignment is done in a class, this has to be placed in the constructor of the class. Click on [|] to download the required extra php-file.

If you want to use this construction send a mail to [2] with the name of the database and website.