套板樣式使用Bootstrap,不知道什麼是Bootstrap的可以自行google一下,然後這邊先直接假設已經在mySQL設定好一組使用者帳號。
資料庫內容大致上是這樣
其中account設定為主鍵
signin.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>管理系統</title>
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<?php
$account=$_POST['account'];
$password=$_POST['password']
?>
</head>
<body>
<div class="col-md-4"></div>
<div class="container col-xs-6 col-md-4">
<form class="form-signin" action="signinResult.php" method="post">
<h2 class="form-signin-heading">後臺登入</h2>
<label for="inputAccount" class="sr-only">Account</label>
<input type="text" id="account" name="account" class="form-control" placeholder="Account" required>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="password" name="password" class="form-control" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
</div>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
"signin.php"只是一個簡單的畫面,登入是否成功的判斷則是交給"signinResult.php"執行
signinResult.php:
<?php
//承接來自上一頁POST過來的資料
$account=$_POST['account'];
$password=$_POST['password'];
//資料庫操作
require("./php/dbCon.php");
$sql = "SELECT * FROM professor_user WHERE account ='$account'";
$result = $conn->query($sql);
$conn->close();
//查詢結果
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo " " . $row["account"]. " " . $row["password"]. " " . $row["name"]. " " . $row["mail"]. "<br>";
$password_c=$row["password"];
session_start();
$_SESSION['name']=$row["name"];
$_SESSION['account']=$row["account"];
$_SESSION['pwd']=$row["password"];
$_SESSION['mail']=$row["mail"];
}
}
else { //查無帳號
echo "帳號或是密碼錯誤,請檢查輸入!";
}
//檢查密碼
if($password==$password_c){
//轉跳到管理首頁
header("location:index.php");
}
else{
echo "帳號或是密碼錯誤,請檢查輸入!";
}
?>
到這邊即完成一個簡單的登入頁面和功能