php连接mysql密码为空,mysql密码用PHP连接到MySQL

In tutrorials for Connecting to MySQL with PHP you see something similar to the below.

$pdo = new PDO('mysql:host=localhost;dbname=mydb', 'myuser','mypassword');

I have a connection working this way on my localhost but for putting it live what do you do about the password? Do you just leave it as plain text like that in your php file? Or is there a more secure way to handle this?

解决方案

Nobody can see your connection string if they look at the source, it can only be seen by looking at your raw code. I would also have it inside a separate file, and include the file on your page. This also helps if you need to change the password, as you won't have to edit every page that uses a connection - you'll only need to edit the one file.

Alternatively, you can have a connection string in an include file and place it outside of document root. This stops people getting to this file using a browser or if they attack your FTP. This will help security of your plain-text passwords, but is still accessible if somebody gets/has access to your local directories. To do this, you may need to configure a PHP configuration variable, open_basedir, which allows your script to talk to a file outside of root. This all depends on if you have access to a folder behind root of course, and if you can change that configuration variable.

Other than that, there's not much that can be done.

Include File Example:

Create a file called conn.php and store your connection in there.

$dbConn = mysql_connect($host, $user, $pass);

mysql_select_db("dbName", $dbConn);

On the page that needs the connection, include the conn.php file like so:

include("conn.php");

if (!dbConn) {

die('Sorry, our database did not load. Please try again later.');

exit();

}

$result = mysql_query("...");

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值