一、主页
<?php
echo '<h1><a href="User/login_html.php">登录</a></h1>';
echo '<h1><a href="User/newUser_html.php">注册</a></h1>';
页面效果: 点击登录跳转到登录页
二、用户
1、登录页面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录页面</title>
</head>
<body align="center">
<div id="all">
<h4>欢迎!请登录</h4>
<br>
<form action="CheckUser.php" method="post">
卡号:<input type="text" name="username" id="" placeholder="请输入银行卡号" />
<br>
密码:<input type="password" name="password" id="" placeholder="请输入银行卡密码" />
<br><br>
<input type="submit" name="" id="" value="登录"/>
</form>
</div>
</body>
</html>
页面效果: 2、从数据库中获取卡号和密码进行登录,在过程中设定好SESSION、COOKIE的值,并进行检查
<?php
include_once '../Tools/DBTools.php';
session_start();
$flag = false;
$message = '';
$username = $_POST['username'];
$password = $_POST['password'];
DBTools::init();
$loginSql = "select *,(select RealName from accountinfo where accountinfo.AccountId=bankcard.AccountId)
RealName from bankcard where CardNo='$username' and CardPwd='$password'";
$result = DbTools::select($loginSql);
if (!empty($result)){
$flag = true;
$message = '登录成功!';
$token = md5($username.$password.time(),"/");
setcookie('Token',$token,time()+3600);
$_COOKIE['Token'] = $token;
$_SESSION[$token]=[
'username'=>$username,
'password'=>$password,
'RealName'=>$result['RealName'],
'CardId'=>$result['CardId']
];
}else{
$message = '登入失败';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>登录验证</title>
</head>
<body>
<input type="hidden" name="" id="flag" value="<? echo $flag;?>" />
<input type="hidden" name="" id="message" value="<? echo $message;?>" />
</body>
<script type="text/javascript">
var flag = document.getElementById('flag').value;
var message = document.getElementById('message').value;
onload = load();
function load() {
alert(message);
if (flag){
location.href='user_center.php';
} else{
<?php echo '111'?>
}
}
</script>
</html>
效果: 3、用户中心
<?php
include_once '../Tools/DBTools.php';
session_start();
include_once '../common/common.php';
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="login_html.php">请先登入</a>';
die;
}
$token = $_COOKIE['Token'];
$RealName = $_SESSION[$token]['RealName'];
$CardNo = $_SESSION[$token]['username'];
DbTools::init();
$sql = "select CardMoney from bankcard where CardId=".$_SESSION[$token]['CardId'];
$result = DbTools::select($sql);
$money = $result['CardMoney'];
DbTools::close();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>用户中心</title>
</head>
<body>
<h3>欢迎您:<?php echo $RealName?></h3>
<br>
<p>卡号:<?php echo $CardNo?></p>
<p>余额:<?php echo $money?></p>
<p>
<a href="../Money/MoneyIn.php">存钱</a>
<a href="../Money/MoneyOut.php">取钱</a>
<a href="../Money/Transfer.php">转账</a>
</p>
<br>
<a href="leave_login.php">退出登录</a>
</body>
</html>
页面效果:
三、数据库连接:封装成类
<?php
class DBTools
{
private static $coon;
private static $host = '127.0.0.1';
private static $user = 'root';
private static $password = 'root';
private static $database = 'bank';
public static function init($host='',$user='',$password='',$database=''){
if(!empty($host)){
self::$host = $host;
}
if(!empty($user)){
self::$user = $user;
}
if(!empty($password)){
self::$password = $password;
}
if(!empty($database)){
self::$database = $database;
}
self::$coon = new \mysqli();
self::$coon->connect(self::$host,self::$user,self::$password,self::$database);
if (!empty(self::$coon->connect_error)){
echo '数据库连接失败:'.self::$coon->connect_error;
die;
}
}
public static function Select($sql){
$data = [];
$result = self::$coon->query($sql);
if (empty(self::$coon->error)){
while (true){
$row = $result->fetch_assoc();
if (empty($row)){
break;
}
$data = $row;
}
}
return $data;
}
public static function noSelect($sql){
$result = self::$coon->query($sql);
if ($result){
if (self::$coon->affected_rows>0){
return [
'status'=>true,
'message'=>''
];
}else{
return [
'status'=>false,
'message'=>'影响数为0'
];
}
}else{
return [
'status'=>false,
'message'=>self::$coon->error
];
}
}
public static function close(){
self::$coon->close();
}
}
四、将页面SESSION、COOKIE值的检查、存取钱方法、获取卡号方法都写入一个文件中
<?php
session_start();
function getLoginStatus(){
if(empty($_COOKIE['Token'])){
return false;
}
$token = $_COOKIE['Token'];
if(empty($_SESSION[$token])){
return false;
}
return true;
}
function MoneyIn_Out($type,$cardId,$money){
if ($type==1){
$sql ="update bankcard set CardMoney=CardMoney+$money where CardId=$cardId";
}else{
$sql ="update bankcard set CardMoney=CardMoney-$money where CardId=$cardId and CardMoney>$money";
}
$result = DbTools::noSelect($sql);
if (!$result['status']){
return $result;
}
if ($type==1){
$sql ="insert into cardexchange(CardId,MoneyInBank,MoneyOutBank,ExchangeTime)
values ($cardId,$money,0,NOW())";
}else{
$sql ="insert into cardexchange(CardId,MoneyInBank,MoneyOutBank,ExchangeTime)
values ($cardId,0,$money,NOW())";
}
$result = DbTools::noSelect($sql);
return $result;
}
function getCardId($CardNum){
$sql ="select CardId from bankcard where CardNo = $CardNum";
$result = DbTools::select($sql);
return $result['CardId'];
}
五、存钱、取钱、转账以及检查
1、存钱页面和检查
<?php
include_once '../tools/DBTools.php';
include_once '../common/common.php';
session_start();
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="../User/login_html.php">请先登入</a>';
die;
}
$token = $_COOKIE['Token'];
$userInfo = $_SESSION[$token];
DbTools::init();
$Sql = "select CardMoney from bankcard where CardId=".$userInfo['CardId'];
$result = DbTools::select($Sql);
$money = $result['CardMoney'];
DbTools::close();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户中心</title>
</head>
<body>
<p><a href="../User/user_center.php">回到个人中心</a></p>
<form action="MoneyInCheck.php" method="post">
当前余额:<input type="text" name="old_money" id="" value="¥<?php echo $money; ?>" readonly="readonly" disabled="disabled" /><br />
存钱金额:<input type="text" name="money" id="" value="" />
<br />
<input type="submit" value="提交"/>
</form>
</body>
</html>
页面效果: 检查代码:
<?php
include_once '../tools/DBTools.php';
session_start();
include_once '../common/common.php';
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="../User/login_html.php">请先登入</a>';
die;
}
$money = $_POST['money'];
if(empty($money) || $money<0){
echo '存款金额有误,<a href="MoneyIn.php">重新存款</a>';die;
}
$token = $_COOKIE['Token'];
$userInfo = $_SESSION[$token];
DbTools::init();
$result = MoneyIn_Out(1,$userInfo['CardId'],$money);
if($result['status']){
echo '存款成功<a href="MoneyIn.php">继续存款</a>';
}else{
echo '存款有误: '.$result['message'];
}
DbTools::close();
2、取钱页面和检查
<?php
include_once '../tools/DBTools.php';
session_start();
include_once '../common/common.php';
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="../User/login_html.php">请先登入</a>';
die;
}
$token = $_COOKIE['Token'];
$userInfo = $_SESSION[$token];
DbTools::init();
$Sql = "select CardMoney from bankcard where CardId=".$userInfo['CardId'];
$result = DbTools::select($Sql);
$money = $result['CardMoney'];
DbTools::close();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户中心</title>
</head>
<body>
<p><a href="../User/user_center.php">回到个人中心</a></p>
<form action="MoneyOutCheck.php" method="post">
当前余额:<input type="text" name="old_money" id="" value="¥<?php echo $money; ?>" readonly="readonly" disabled="disabled" /><br />
取钱金额:<input type="text" name="money" id="" value="" />
<br />
<input type="submit" value="提交"/>
</form>
</body>
</html>
页面效果: 检查代码:
<?php
include_once '../tools/DBTools.php';
session_start();
include_once '../common/common.php';
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="../User/login_html.php">请先登入</a>';
die;
}
$money = $_POST['money'];
if(empty($money) || $money<0){
echo '取款金额有误,<a href="MoneyOut.php">重新取款</a>';die;
}
$token = $_COOKIE['Token'];
$userInfo = $_SESSION[$token];
DbTools::init();
$result = MoneyIn_Out(2,$userInfo['CardId'],$money);
if($result['status']){
echo '取款成功<a href="MoneyOut.php">继续取款</a>';
}else{
echo '取款有误: '.$result['message'];
}
DbTools::close();
3、转账代码和检查
<?php
include_once '../tools/DBTools.php';
session_start();
include_once '../common/common.php';
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="../User/login_html.php">请先登入</a>';
die;
}
$token = $_COOKIE['Token'];
$userInfo = $_SESSION[$token];
DbTools::init();
$Sql = "select CardMoney from bankcard where CardId=".$userInfo['CardId'];
$result = DbTools::select($Sql);
$money = $result['CardMoney'];
DbTools::close();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用户中心</title>
</head>
<body>
<p><a href="../User/user_center.php">回到个人中心</a></p>
<form action="TransferCheck.php" method="post">
当前余额:<input type="text" name="old_money" id="" value="¥<?php echo $money; ?>" readonly="readonly" disabled="disabled" /><br />
转账金额:<input type="text" name="money" id="" value="" /><br/>
对方卡号:<input type="text" name="Op_CardNo" id="" value="" />
<br />
<input type="submit" value="提交"/>
</form>
</body>
</html>
页面效果: 检查代码:
<?php
include_once '../tools/DBTools.php';
session_start();
include_once '../common/common.php';
$is_login = getLoginStatus();
if (!$is_login){
echo '尚未登入,<a href="../User/login_html.php">请先登入</a>';
die;
}
$money = $_POST['money'];
$Op_CardNo = $_POST['Op_CardNo'];
if(empty($money) || $money<0){
echo '转帐金额有误,<a href="Transfer.php">重新转帐</a>';die;
}
if(empty($Op_CardNo) ){
echo '对方卡号有误,<a href="Transfer.php">重新转帐</a>';die;
}
$token = $_COOKIE['Token'];
$userInfo = $_SESSION[$token];
$CarNum=$_SESSION[$token]['username'];
$CarId = $userInfo['CardId'];
DbTools::init();
$result = MoneyIn_Out(2,$CarId,$money);
if($result['status']){
}else{
echo '转账有误: '.$result['message'];
}
$CarNum2= $_POST['Op_CardNo'];
$CarId2 =getCardId($CarNum2);
$result = MoneyIn_Out(1,$CarId2,$money);
if($result['status']){
}else{
echo '转账有误: '.$result['message'];
}
$id1=$CarId;
$id2=$CarId2;
$sql1="INSERT INTO cardtransfer(cardtransfer.CardIdOut,cardtransfer.CardIdIn,cardtransfer.TransferMoney,cardtransfer.TransferTime)
VALUES($id1,$id2,$money,NOW())";
$result=DbTools::noSelect($sql1);
if($result['status']){
echo '转账成功,<a href="Transfer.php">重新转帐</a>';
}else{
echo '转账有误: '.$result['message'];
}
DbTools::close();
|