1

可能重复:
如何在 PHP 代码中安全地存储密码?

所以这是我有一个 C# 程序的场景。它会将序列号发送到我的网站。(例如:www.example.com/LicenseCheck.php)可以说它以串行方式发送 1234-1234-1234。

然后 licenseCheck.php 将连接到我的 mysql:

<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>

然后它将从有效的序列表中进行“选择”,如果表中存在许可证,则表示它是有效的序列。

基本上,我试图找到什么是解决这个问题的好方法。我最担心在哪里存储“用户名”、“密码”。基本上我只有一个用户只有读取权限。

我想尽可能地隐藏用户名和密码,并希望以安全的方式解决这个问题。

“安全”是什么意思?只是一种防止未经授权的用户进入我的数据库的方法。

感谢所有的帮助。

4

2 回答 2

1

mysql 功能不再使用。我将为您提供解决方案的 mysqli 程序方法。我还将介绍一种保存密码的安全方法。

基本的mysqli连接和查询:

$queryString = "SELECT COUNT(*) FROM table WHERE userName = ? AND password=?";//not the question marks
//the question marks are used to bind our parameters/variables so the query string cannot be adjusted I believe? 
$DBH=mysqli_connect('local','user','pass');
mysqli_select_db($DBH,'database');
$query = mysqli_prepare($DBH,$queryString);
mysqli_bind_param($query,'ss',$username,$passwordHASH);//the second parameter declares the datatypes 
mysqli_execute($query);
//mysqli_bind_result($results); ||not needed in this situation but this is how you'd get results from the database
if(mysqli_fetch($query)){ //adding  && $results = 1 would insure there is only one record
    //username and password match
}
else{
    //error builder etc
}

现在对于您的密码,我建议使用 ahmac_hash()来提供保护。将您的密码插入数据库时​​,请确保hmac_hash()在您查询数据库时,这些密码受到保护。

$passwordHASH = hash_hmac('sha512',$hashThis, '&R4nD0m^');

所以基本上你的查询/获取将返回 true 如果COUNT(*)返回 1 如果用户名和密码等于$usernameand $passwordHASH

但是说了这么多,你不能#include <mysql.h>在 C# 中使用 mysql 库吗?

于 2013-01-19T09:47:32.093 回答
0

看看这个答案的第二个要点。本质上,您需要将 MySQL 连接详细信息放在文档根目录之外的配置文件中(例如 public_html/ 等),然后需要该文件。

于 2013-01-19T09:36:01.993 回答