第104、105講 僱員管理系統②③-model1模式簡單登陸+資料庫登陸
阿新 • • 發佈:2018-11-20
login.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>管理員登陸</title >
</head>
<h1>管理員登陸</h1>
<body>
<form method="post" action="LoginProcess.php">
<table border='1px' width='400px'>
<tr>
<td>使用者登陸</td>
</tr>
</table>
<br /> 使用者名稱:<input type="text" name='username' /><br /> 密 碼:<input
type="password" name='password' /><br /> 是否要儲存cookies<input
type="checkbox" name="iscookies" /><br /> <input type="submit"
value='提交' />
</form>
</body>
</html>
LoginProcess.php
<?php
header("content-type:text/html;charset=utf-8");
include 'Constant.php';
$username = $_POST['username'];
$password = $_POST['password'];
echo "<br/>";
echo "username " . $username;
echo "<br/>";
echo "password " . $password;
echo "<br/>";
$mysqli = new mysqli(localhost, hostname, password, dbname);
if ($mysqli->connect_error) {
echo "資料庫連接出錯" . $mysqli->connect_error;
exit();
}
$sql = "select password from admin where name='" . $username . "'";
$res = $mysqli->query($sql);
echo "<pre>";
var_dump($res);
echo "</pre>";
$rows = $res->num_rows;
echo "rows = " . $rows;
if ($rows > 0) {
for ($i = 0; $i < $rows; $i ++) {
$result = $res->fetch_assoc();
echo "密碼 " . $result["password"];
if ($result["password"] == md5($password)) {
echo "可以登陸";
header("Location:Main.php?name='admin'");
exit();
} else {
echo "不可以登陸";
exit();
}
}
}else {
echo "當前使用者不存在,請註冊";
}