接着之前那篇 “電子商城項目開發(后台功能模塊開發)”
登陸頁面login.php
<?php require '../tools.func.php'; require '../db.func.php'; // 判斷當前是否為post提交 if (!empty($_POST['username'])) { $action = htmlentities($_GET['action']); $prefix = getDBPrefix(); if ($action == 'reg') { $username = htmlentities($_POST['username']); $password = md5(htmlentities($_POST['password'])); $email = htmlentities($_POST['email']); $created_at = date('Y-m-d H:i:s'); $sql = "INSERT INTO {$prefix}user(username, password, email, created_at) VALUES('$username', '$password', '$email', '$created_at')"; if (execute($sql)) { setInfo('注冊成功'); } else { setInfo('注冊失敗'); } } elseif ($action == 'login') { $username = htmlentities($_POST['username']); $password = md5(htmlentities($_POST['password'])); $sql = "SELECT id, username FROM {$prefix}user WHERE username = '$username' AND password = '$password'"; $res = queryOne($sql); if ($res) { setSession('shop', ['username' => $username, 'id' => $res['id']]); header('location: index.php'); } else { setInfo('用戶名或者密碼錯誤'); } } } // 判斷操作為login 還是 reg // 如果是reg,要接收post數據,插入新數據 // 如果是login,要查詢數據庫,判斷用戶名或者密碼是否正確,正確的話寫入session // 顯示結果 require 'header.php'; ?> <!-- Start Login Register Area --> <div class="htc__login__register bg__white ptb--130"> <div class="container"> <div class="row"> <div class="col-md-6 col-md-offset-3"> <ul class="login__register__menu" role="tablist"> <li role="presentation" class="login active"><a href="#login" role="tab" data-toggle="tab">登錄</a></li> <li role="presentation" class="register"><a href="#register" role="tab" data-toggle="tab">注冊</a></li> </ul> </div> </div> <!-- Start Login Register Content --> <div class="row"> <div class="col-md-6 col-md-offset-3"> <div class="htc__login__register__wrap"> <!-- Start Single Content --> <div id="login" role="tabpanel" class="single__tabs__panel tab-pane fade in active"> <p><?php if (hasInfo()) echo getInfo(); ?></p> <form id="loginform" class="login" method="post" action="login.php?action=login"> <input type="text" name="username" placeholder="User Name*"> <input type="password" name="password" placeholder="Password*"> </form> <div class="htc__login__btn mt--30"> <a href="javascript:document.getElementById('loginform').submit();">登錄</a> </div> </div> <!-- End Single Content --> <!-- Start Single Content --> <div id="register" role="tabpanel" class="single__tabs__panel tab-pane fade"> <p><?php if (hasInfo()) echo getInfo(); ?></p> <form id="regform" class="login" action="login.php?action=reg" method="post"> <input type="text" name="username" placeholder="Name*"> <input type="email" name="email" placeholder="Email*"> <input type="password" name="password" placeholder="Password*"> </form> <div class="htc__login__btn"> <a href