Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to connect to MySQL with PHP securely [duplicate]

Tags:

I want some input on what you guys think is the most secure way to connect to a MySQL database using PHP. Currently the way I'm doing it is a utility PHP file that I include in the top of all my other PHP files. The utility PHP file is this:

<?php     if(!defined('IN_PHP')){         die("hackerssss");     }     $mysql_host = "localhost";     $mysql_user = "root";     $mysql_pass = "root";     $mysql_db = cokertrading; ?> 

Any suggestions?

like image 998
Tim Avatar asked Sep 14 '10 15:09

Tim


People also ask

Which of the following methods connect MySQL database using PHP?

There are two popular ways to connect to a MySQL database using PHP: With PHP's MySQLi Extension. With PHP Data Objects (PDO)

How connect MySQL database from another server in PHP?

In the control panel's Homepage go the databases section and click the Remote MYSQL option. Then add the Ip address of the Server A and click on add host. Now you can access to the database in Server B while your scripts are running in Server A.


2 Answers

Suggestion: You should probably never be running as root; create another account and give it the 'least' privileges required for your site.

like image 114
Alex Avatar answered Oct 07 '22 18:10

Alex


I can believe noone has mentioned MYSQLI and prepared statements yet, you may lock your password and database connection away, but thats ultimately futile if I can simply type ';DROP TABLE users;-- in the login form.

Check http://en.wikipedia.org/wiki/SQL_injection for an idea about what I'm talking about.

like image 24
Kristoffer Sall-Storgaard Avatar answered Oct 07 '22 18:10

Kristoffer Sall-Storgaard