結構
- MySQL會員資料庫(member)
- 登入頁面(login.php)
- 註冊頁面(regist.php)
- 會員頁面(member.php)
MySQL會員資料庫結構(member)
編碼:utf8_unicode_ci
名稱 | 型態 | 備註 |
id | int | AI |
name | text | |
text | ||
password | text | |
nick | text |
登入頁面 login.php
<?php
//解決網頁亂碼問題
mysql_query("SET NAMES UTF8");
$error_flag = FALSE;
$notfound_flag = FALSE;
//對資料庫伺服器進行連線,並選擇對應的會員資料庫
mysql_connect("localhost", "root", "");
mysql_select_db("hahow");
//如果收到 POST 表單送來的登入資料,到資料庫檢査是否有這個人存在
//(使用 mysql_query("SELECT ...... "),然後把回傳的東西透過 mysql_fetch_array(......) 來檢査)
$result=mysql_query("SELECT * FROM member");
//如果有找到,檢査密碼是否相符
while($row = mysql_fetch_array($result)){
//先檢査使用者有沒有輸入資料
if(empty($_POST["email"])==FALSE && empty($_POST["pass"])==FALSE){
//防範攻擊
$userEmail=$_POST["email"];
$userEmail=mysql_real_escape_string($userEmail);
$userPassword=$_POST["pass"];
$userPassword=mysql_real_escape_string($userPassword);
//有輸入資料的話,再來看輸入的email跟資料庫是否一致
if($row["email"]==$_POST["email"]){
if($row["password"]==$_POST["pass"]){
//如果相符合,則設定 Session(記得要先 session_start()!),並轉址到會員中心(member.php)
session_start();
$_SESSION["email"]=$_POST["email"];
$_SESSION["password"]=$_POST["pass"];
$_SESSION["name"]=$row["name"];
$_SESSION["id"]=$row["id"];
//讓網頁轉址的 PHP 寫法:header('Location: member.php');
header('Location: member.php');
}else{
//如果不符合,則設定 $error_flag 為 TRUE,繼續顯示網頁内容
$error_flag = TRUE;
break;
}
}else{
//如果沒有找到,則設定 $notfound_flag 為 TRUE,繼續顯示網頁内容
$notfound_flag = TRUE;
}
}else{
//如果沒收到,繼續顯示網頁内容
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>會員登入</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<br><br><br><br>
<div class="container">
<div class="row jumbotron">
<div class="col-md-6 col-md-offset-3">
<h2 class="text-center">會員登入</h2><br/>
<form action="<?php echo htmlspecialchars($_SERVER[">" method="POST">
<input class="form-control input-lg" id="pass" type="text" name="email" required="TRUE" placeholder="E-Mail"/><br/>
<input class="form-control input-lg" id="pass" type="password" name="pass" required="TRUE" placeholder="密碼"/><br/>
<input class="btn btn-primary btn-lg btn-block" type="submit" value="登入"/><a class="btn btn-default btn-lg btn-block" href="register.php">會員註冊</a>
</form>
<br/>
<?php if($error_flag){ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> 密碼錯誤!
</div>
<?php }?>
<?php if($notfound_flag){ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button class="close" type="button" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button> 未找到本使用者,請重新確認!
</div>
<?php }?>
</div>
</div>
</div>
</body>
</html>
會員頁面 member.php
<?php
//關閉系統提示
error_reporting(0);
session_start();
// 檢査是否有登入(Session 有被設定)
if(isset($_SESSION["email"])==FALSE) {
//如果沒有,轉址到登入頁面
header('Location: login.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>會員中心</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<br><br><br><br>
<div class="container">
<div class="row jumbotron">
<div class="col-md-6 col-md-offset-3">
<h2 class="text-center">會員登入</h2>
<!-- 在這裡請把 HTML 修改一下,讓他可以顯示出現在登入的會員姓名! -->
<!-- 提示:先取用 Session 中登入的人的 ID,然後再去資料庫中找出對應 ID 的姓名。 -->
<h3>姓名:<?php echo $_SESSION["name"];?></h3>
<a class="btn btn-primary btn-lg btn-block" href="msg.php">留言板</a>
</div>
</div>
</div>
</body>
</html>
註冊頁面 register.php
<?php
$noInfo_flag = false;
$duplicate_flag = false;
$success_flag=false;
$fail_flag=false;
//關閉提示
error_reporting(0);
mysql_connect("localhost", "root", "");
mysql_select_db("hahow");
mysql_query("SET NAMES UTF8");
//送出鍵按出後
if(isset($_GET['submit'])==true){
//檢査所有欄位有沒有輸入
if(empty($_GET['email'])==true || empty($_GET['password'])==true || empty($_GET['name'])==true || empty($_GET['nick'])==true){
//有缺的話,叫使用者寫完
$noInfo_flag = true;
}
}
//送出鍵按出,使用者也有輸入資料的情況
if(isset($_GET['submit'])==true && empty($_GET['email'])==false && empty($_GET['password'])==false && empty($_GET['name'])==false && empty($_GET['nick'])==false){
//用 WHERE 檢查是否重複註冊
//mysql_query()裡面要用'' 參考
$result=mysql_query("SELECT * FROM member WHERE email='$_GET[email]'");
$row=mysql_fetch_array($result);
if($row["email"]==$_GET["email"]){
重覆到的話,退回
$duplicate_flag = true;
}else{
//沒有重複到,寫入資料
$SaveNewData=mysql_query("INSERT INTO member (name, email, password, nick) VALUES('$_GET[name]','$_GET[email]','$_GET[password]','$_GET[nick]')");
//檢查註冊是否成功
if(!$SaveNewData){
$fail_flag=true;
}else{
$success_flag=true;
}
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>會員註冊</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<br><br><br><br>
<div class="container">
<div class="row jumbotron">
<div class="col-md-6 col-md-offset-3">
<h2 class="text-center">會員註冊</h2>
<hr>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="GET">
<div class="form-group">
<label for="input-email">Email 帳號 *</label>
<input type="email" class="form-control" id="input-email" name="email">
</div>
<div class="form-group">
<label for="input-name">真實姓名 *</label>
<input type="text" class="form-control" id="input-name" name="name">
</div>
<div class="form-group">
<label for="input-nick">匿稱 *</label>
<input type="text" class="form-control" id="input-nick" name="nick">
</div>
<div class="form-group">
<label for="input-password">密碼 *</label>
<input type="password" class="form-control" id="input-password" name="password">
</div>
</div>
<br>
<input type="submit" class="btn btn-primary btn-lg btn-block" value="註冊" name="submit">
</form>
<?php if($noInfo_flag){ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
請輸入所有欄位!
</div>
<?php }?>
<?php if($duplicate_flag){ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
此 Eamil 帳號已經註冊過!
</div>
<?php }?>
<?php if($success_flag){ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
註冊成功!
</div>
<?php }?>
<?php if($fail_flag){ ?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
註冊失敗!
</div>
<?php }?>
</div>
</div>
</div>
</body>
</html>
範例:會員登入網站
測試用帳號:123@123
測試用密碼:123
接續後篇:會員留言板系統