简易数据库加密系统

总述

设计实现一个简易的数据库加密系统,实现数据库加密,数据库查询,密钥管理等模块。前期想法是准备写一个数据管理系统,实现对数据库的增删查改操作,并在其中使用密钥实现对数据库的加密。流程图如下,在进行增删查改等操作的过程中需要输入密钥来对数据经行加密和解密。本系统基于phpstudy,建立在本地上。

1,实现用户登陆

效果图

login.php代码:

<?php
include "./connect.php";
//接收数据
if(isset($_POST['userid']) && isset($_POST['password'])){
	//从数据库里查找用户名是否存在
	$_sql = "SELECT user_id,passwd FROM people WHERE user_id='{$_POST['userid']}'";
	$result = _fetch_array($_sql);
	if(!empty($result[0])){
		if($_POST['password']==$result[0]['passwd']){
                                    if($_POST['userid'] == "admin")
                                         { 
                                              _location('','admin.php');
                                         }
                                         else{
			_location('','hello.php');    //示例网站
                                         }
		}else if($_POST['password']==''){
			_alert('密码为空,请输入密码');
		}
		else{
			_alert('密码错误');
		}
	}else if($_POST['userid']=='' && $_POST['password']==''){
		_alert('用户名和密码为空,请输入用户名和密码');
	}else if($_POST['userid']==''){
		_alert('用户名为空,请输入用户名');
	}else {
		_alert('用户名不存在');
	}
	_close();
	exit;
}
?>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="login.css" >
</head>

<body>
    <div id="main">

        <div id="avatar">
        </div>
             <form action="login.php" method="post">
		 用户名:<input type="text" name="userid" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
		 密码:<input type="password" name="password" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
		<input type="submit" value="提交" style="width: 75%;height: 40px;display: block;margin: 30px auto;background:#ffc5d1 ;border: none;outline: none;color: #fff;font-size: 16px;cursor: pointer;">
	</form>


        <div id="footer">
            <a href="#">Forget Password?</a>
        </div>
    </div>
</body>

 login.css

@charset "utf-8";
/*统一设置*/
*{
    margin: 0;
    padding: 0;
}
/*网页背景图片*/
body{
    background: url("picture/background.jpg") no-repeat ;
    background-size: cover;
}
/*登陆区主体*/
#main{
    width: 350px;
    height: 600px;
    background: rgba(0,0,0,0.5);
    margin: 40px auto;
    border-top: 8px solid #ffc5d100;
    position: relative;
}

/*头像区*/
#avatar{
    width: 184px;
    height: 184px;
    background: url("picture/avatar.jpg") no-repeat;
    background-size: cover;
    margin: 50px auto;
    border-radius: 50%;

}

/*登陆区底部*/
#footer{
    height: 50px;
    text-align: center;
    line-height: 50px;
    position: absolute;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #ccc;
}
#footer a{
    color: #ccc;
    text-decoration: none;
}
#footer a:hover{
    color: red;
}

connect.php(数据库连接代码)

<?php
	$_conn=mysqli_connect('localhost','root','root'); //主机地址 用户名 密码 如果你的跟我一样就不用改
	if (!$_conn) {
		exit('数据库连接失败:'.mysqli_error($_conn));
	}
	mysqli_select_db($_conn,'user')or die('找不到数据库:'.mysqli_error($_conn).mysqli_errno($_conn));
	mysqli_query($_conn,"SET NAMES UTF8");
	// var_dump($_conn);
	include "sql.func.php";
 ?>

 sql.func.php (底层封装文件 不是很懂)

<?php
/**
*弹框
*/
function _alert($_info){
    echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
    exit;
}
 
/**
 * _location():弹出一个对话框并且转跳到另一个界面
 */
function _location($_info,$_url){
    if($_info==null){
        header('Location:'.$_url);
    }else{
        echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";
        exit;
    }
 
}
 
/**
 * _connect():连接数据库
 */
function _connect()
{
    //定义全局变量$_conn,在函数外部也能调用
    global $_conn;
    $_conn=mysqli_connect(DB_HOST, DB_USER,DB_PWD);
    if (!$_conn) {
        exit('数据库连接失败:'.mysqli_error($_conn));
    }
}
 
/**
 * _select_db():选择数据库
 */
function _select_db(){
    global $_conn;
    if(!mysqli_select_db($_conn,DB_NAME)){
        exit('找不到数据库'.mysqli_error($_conn));
    }
}
 
/**
 * _set_names():设置字符编码
 */
function _set_names(){
    global $_conn;
    if(!mysqli_query($_conn,'SET NAMES UTF8')){
        exit('字符编码错误'.mysqli_error($_conn));
    }
}
 
/**
 * _query():执行sql语句
 * @param string $_sql sql操作语句
 * @return string 返回结果集
 */
function _query($_sql){
    global $_conn;
    if(!$result=mysqli_query($_conn,$_sql)){
        exit('SQL执行失败'.mysqli_error($_conn).mysqli_errno($_conn));
    }
    return $result;
}
 
/**
 * _fetch_array():根据sql语句遍历数据库。返回一个数组,键名是数据库的表单结构名
 * @param string $_sql sql操作语句
 * @return array|null
 */
function _fetch_array($_sql){
    return mysqli_fetch_all(_query($_sql),MYSQLI_ASSOC);
}
 
/**
 * _num_rows():返回数据库中查找条件的数据个数
 * @param string $_sql sql操作语句
 * @return int 返回数据个数
 */
function _num_rows($_sql){
    return mysqli_num_rows(_query($_sql));
}
 
/**
 * _affected_rows():返回数据库里被影响到的数据条数
 * @return int 返回影响到的记录数
 */
function _affected_rows(){
    global $_conn;
    return mysqli_affected_rows($_conn);
}
 
/**
 * _is_repeat():判断数据在数据库里是否已经存在
 * @param string $_sql sql操作语句
 * @param string $_info 弹窗上显示的文字
 */
function _is_repeat($_sql,$_info){
    if(_fetch_array($_sql)){
        _alert_back($_info);
    }
}
 
/**
 * _close():关闭数据库
 */
function _close(){
    global $_conn;
    if(!mysqli_close($_conn)){
        exit('数据库关闭异常'.mysqli_error($_conn));
    }
}
?>

index.php 文件

<?php
session_start();//开启Session功能。
include "php/connect.php";
//接收数据
if(isset($_POST['user_id']) && isset($_POST['password'])){
	//从数据库里查找用户名是否存在
                $mdpw=substr(md5($_POST['password']),8,16);//对用户输入密码进行16位MD5加密。
	$_sql = "SELECT passwd FROM people WHERE user_id='{$_POST['user_id']}'";
	$result = _fetch_array($_sql);
	if(!empty($result[0])){
		if($mdpw==$result[0]['passwd']){
                                    if($_POST['user_id'] == "admin")
                                         { 
                                             $_SESSION['user_id']="{$_POST['user_id']}";
                                              _location('','php/admin.php');//管理员界面
                                         }
                                         else{
                                                 $_SESSION['user_id']="{$_POST['user_id']}";
			_location('','php/user.php');    //用户界面
                                         }
		}else if($_POST['password']==''){
			_alert('密码为空,请输入密码');
		}
		else{
			_alert('密码错误');
		}
	}else if($_POST['userid']=='' && $_POST['password']==''){
		_alert('用户名和密码为空,请输入用户名和密码');
	}else if($_POST['userid']==''){
		_alert('用户名为空,请输入用户名');
	}else {
		_alert('用户名不存在');
	}
	_close();
	exit;
}
?>
<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title>Login</title>
    <link rel="stylesheet" href="css/login.css" >
</head>

<body>
    <div id="main">
    <div id="avatar">
        </div>
             <form action="index.php" method="post">
		 用户名:<input type="text" name="user_id" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
		 密码:<input type="password" name="password" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
		<input type="submit" value="提交" style="width: 75%;height: 40px;display: block;margin: 30px auto;background:#ffc5d1 ;border: none;outline: none;color: #fff;font-size: 16px;cursor: pointer;">
	</form>


        <div id="footer">
            <a href="#">Forget Password?</a>
        </div>
    </div>
</body>

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值