晚上忽然想想,不能一直讓資料庫直接存入使用的密碼,世風日下、盜賊猖狂,該保護的我們是該保護一下。以下正題
1.先建立一個名為form_input.php的檔案
建置以下內容:
<?PHP
//建立資料庫連線
$db_host = "localhost";
$db_table = "test";
$db_username = "root";
$db_password = "123";
if(!@mysql_connect($db_host,$db_username,$db_password)) die("資料庫連接失敗");
if(!@mysql_select_db($db_table)) die ("資料庫選擇失敗");
mysql_query("SET NAMES utf8;");
//參數回傳
$account = $_POST['account'];
$password = md5($_POST['password']);//此為重點,將密碼加密
//傳入資料庫
$sql_query_add = "INSERT INTO md5 (account,password) values ('$account','$password')";
$result_add = mysql_query($sql_query_add);
?>
<!--html 建置 新增一筆紀錄-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>寫入資料庫</title>
</head>
<body>
<form action="form_input.php" method="post">
<table width="340" border="0">
<tr>
<td> </td>
<td>帳號</td>
<td><label for="textfield"></label>
<input type="text" name="account" id="account" /></td>
</tr>
<tr>
<td> </td>
<td>密碼</td>
<td><label for="textfield2"></label>
<input type="text" name="password" id="textfield2" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="button" id="button" value="送出" /></td>
</tr>
</table>
</form>
</body>
</html>
2.再建立一個名為check.php的檔案,來確認驗證
//建立資料庫連線
<?
$db_host = "localhost";
$db_table = "test";
$db_username = "root";
$db_password = "123";
if(!@mysql_connect($db_host,$db_username,$db_password)) die("資料庫連接失敗");
if(!@mysql_select_db($db_table)) die ("資料庫選擇失敗");
mysql_query("SET NAMES utf8;");
$account = $_POST['account'];
$password = md5($_POST['password']);//此為重點,將密碼加密
//讀取資料庫對應帳號與密碼
$sql_query_pro = "select account,password from md5 where account = '$account' and password = '$password'";
$result_pro = mysql_query($sql_query_pro);
list($account,$password) = mysql_fetch_row($result_pro);
if($account != ""){
echo "ok";//如驗證過回傳"ok"
}else{
echo "錯誤";//如驗證不過回傳"錯誤"
}
?>
<!--html 建置 登入測試-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登入測試頁</title>
</head>
<body>
<form action="check.php" method="post">
<table width="340" border="0">
<tr>
<td> </td>
<td>帳號</td>
<td><label for="textfield"></label>
<input type="text" name="account" id="account" /></td>
</tr>
<tr>
<td> </td>
<td>密碼</td>
<td><label for="textfield2"></label>
<input type="text" name="password" id="textfield2" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="button" id="button" value="送出" /></td>
</tr>
</table>
</form>
</body>
</html>