注册系统主页html代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="大眼仔~旭" />
<title >用户注册系统</title>
</head>
<body>
<div style=" background-image:url(7777.png); height: 480px; width: 1210px; padding:10% 10%">
设置盒子的背景图,高度,宽度,内边距
<div style="margin :0% 0% 0% 20%;width:60%; padding:5% 0% 5% 10%; background: #00AA88;">
设置盒子的外边距,宽度,内边距,盒子的背景颜色
<h2 style="color: #fff" >
二级标签
用户注册系统
</h2>
<a href="register.php" style="color: #fff" >用户注册 </a>
超链接标签,链接的路径,设置连接文字的颜色
<a href="login.html" style="color: #fff">用户登录</a>
<a href="upload.php" style="color: #fff">上传文件</a>
</div>
</div>
</body>
</html>
include()函数引用的连接数据库php文件代码:
<?php
$conn=mysqli_connect('127.0.0.1','root','') or die('数据库无法连接');
连接数据库,登录phpmyadmin
$db=mysqli_select_db($conn,'user') or die('选择数据库不存在');
选择需要编辑的数据库
mysqli_set_charset($conn,'utf8');
设置编译码,防止乱码
?>
用户注册功能html代码
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="大眼仔~旭" />
<title>用户注册</title>
</head>
<body>
<div style=" background-image:url(7777.png); height: 480px; width: 1210px; padding:10% 10%">
<div style="margin :0% 0% 0% 20%;width:60%; padding:5% 0% 5% 10%; background: #00AA88;">
<h1 style="margin :0% 30%; color: #fff;">用户注册</h1>
<br />
<form action="toReg.php" method="post" onsubmit="return InputCheck(this);" style="margin :0% 0% 0% 10%">
//return 函数处,如果返回值是两个及以上的必须在每个函数后加分号
<table borde='2' width='70%' cellspacing='3' cellpading='3'
bgcolor='#abcdef' style="color: #000;">
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="username" id="username"/>
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="password" name="pw" id="password"/>
</td>
</tr>
<tr>
<td>
确认密码
</td>
<td>
<input type="password" name="cpw" id="conpassword"/>
</td>
</tr>
<tr>
<td>
邮箱
</td>
<td>
<input type="text" name="email"/>
</td>
</tr>
<tr>
<td>
性别
</td>
<td>
<input type="radio" name="sex" value='male' checked />男
<input type="radio" name="sex" value='female'/>女
</td>
</tr>
<tr>
<td>
爱好
</td>
<td>
<input type="checkbox" name="fav[]" value='football' checked />足球
<input type="checkbox" name="fav[]" value='music'/>音乐
<input type="checkbox" name="fav[]" value='basketball'/>篮球
</td>
</tr>
</table>
<div style="margin :0% 0% 0% 20%; padding:0% 0% 0% 29%;">
<input type="submit" value="注册"/>
<a href="login.html"> <input type="button" value="登录" /> </a>
<input type="reset" value="清空"/>
</div>
</form>
</div>
</div>
</body>
<script>
function InputCheck(temp){
// var username=document.getElementById('username').vale.trim();
// var password=document.getElementById('password').vale.trim();
// var conpassword=document.getElementById('conpassword').vale.trim();
if(temp.username.value==''){
alert('用户名不能为空');
temp.username.focus();
return false;
}
if(temp.password.value==''||temp.conpassword.value==''){
alert('密码不能为空');
temp.password.focus();
return false;
}
判断从前端接收的username,password,conpassword是否为空
if(temp.password.value!=temp.conpassword.value){
alert('两次密码不一致');
temp.password.focus();
return false;
}
return ture;
}
</script>
</html>
<?php
?>
用户注册表单提交到后端php代码:
<?php
/**
* @author 大眼仔~旭
* @copyright 2022
*/
前端使用post传输,后端使用post接收
$username =trim($_POST['username']);
$password =trim($_POST['pw']);
$conpassword =trim($_POST['cpw']);
$email =trim($_POST['email']);
//trim()函数的作用:将接收到的值前后两端的空格去掉
$sex =$_POST['sex'];
$fav1 =$_POST['fav'];
fav接受的是复选框的内容,是一个数组
$fav=implode(',',$fav1);
//将数组的元素通过特定的字符串进行连接,变成一个新的字符串
//后端判断代码
//if($username==''){
// echo "<script>
// alert('用户名不能为空');
// history.back();
// </script>";
// exit;
//}
//if($password=='' or $conpassword==''){
// echo "<script>
// alert('密码不能为空');
// history.back();
// </script>";
// exit;
//}
//if($password !=$conpassword){
// echo "<script>
// alert('密码不一致');
// history.back();
// </script>";
// exit;
//}
include_once('connect.php');
引入文件函数,引入连接数据库文件:connect.php
//通过引用文件函数,引用连接数据库的代码
$sqlselect="select * from userinfo where username = '$username'";
数据库查询操作,用于判断用户名($username)是否存在
$result=mysqli_query($conn,$sqlselect);
数据库执行函数,执行数据库操作的语句
$num=mysqli_num_rows($result);
获取数据库查询结果条数
if($num){
echo "<script>
alert('用户名已存在');
history.back();
</script>";
history.back返回到上一步操作,即重新注册
}else{
$password = md5(trim($_POST['pw']));
密码加密函数md5(),去掉前后多余空格函数trim()
$sqlinsert="insert into userinfo(username,password,email,sex,fav)
values('$username','$password','$email','$sex','$fav')";
$resultinsert=mysqli_query($conn,$sqlinsert);
//将插入数据库语句执行
if($resultinsert){
echo "<script>
alert('注册成功');
location.href='login.html';
</script>";
location.href代表点击确定,跳转到指定路径,此处为:login.html文件
}
else{
echo "<script>
alert('注册失败');
history.back()</script>";
}
}
?>
登录界面html代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="大眼仔~旭" />
<title>无标题 1</title>
</head>
<body>
<div style=" background-image:url(7777.png); height: 480px; width: 1210px; padding:10% 10%">
<div style="margin :0% 0% 0% 20%;width:60%; padding:5% 0% 5% 10%; background: #00AA88;">
<h1 style="color: #fff;">用户登录</h1>
<form action="toLogin.php" method="post" onsubmit ="return InputCheck(this);">
<table borde='1' width='50%' cellspacing='0' cellpading='0' bgcolor='#abcdef'>
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="username" id="username"/>
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="password" name="pw" id="password"/>
</td>
</tr>
</table>
<div style="margin :0% 0% 0% 20%; padding:0% 0% 0% 17.5%;">
<input type="submit" value="登录" />
<a href="reg.htm"> <input type="button" value="注册" /></a>
</div>
</form>
</div>
</div>
</body>
<script>
function InputCheck(temp){
if(temp.username.value == ""||temp.password.value==""){
alert("用户名不能为空");
return false;
}
if(temp.password.value==""){
alert("密码不能为空!");
return false;
}
return turn;
}
</script>
</html>
登录界面登录请求提交到后端处理的php代码:
<?php
/**
* @author 大眼仔~旭
* @copyright 2022
*/
session_start();
//使用全局变量session必须先引用此函数
include_once('connect.php');
使用引用函数,连接数据库
$username=trim($_POST['username']);
$password=md5(trim($_POST['pw']));
将前端接收的密码进行加密
//判断网页输入的密码与数据库中的密码是否一致,加密的密码需要与加密的密码进行匹配
$sqlselect="select * from userinfo where username='$username'and password='$password' ";
$result=mysqli_query($conn,$sqlselect);
if(mysqli_num_rows($result)){
$_SESSION['logged']=$username;
//全局变量记录用户名,键名自己随便取,此变量默认存在时间为1440秒
//可以防止未登录,直接修改密码,上传文件等操作
echo "<script>alert('登陆成功');location.href='upload.php';</script>";
确定后跳转到内部的上传文件界面
}else{
echo "<script>alert('登陆失败!用户不存在或密码不正确!');
history.back()</script>";
}
?>
上传文件界面的html和php合一代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="大眼仔~旭" />
<title>无标题 1</title>
</head>
<body>
<div style=" background-image:url(7777.png); height: 480px; width: 1210px; padding:10% 10%">
<div style="margin :0% 0% 0% 20%;width:60%; padding:5% 0% 5% 10%; background: #00AA88;">
<div style="margin:margin :0% 0% 0% 20%; padding:0% 0% 0% 75%;">
<a href='passwordmodify.html' style="color: #fff; ">修改密码 </a>
<a href='logout.php' style="color: #fff;">退出</a>
</div>
<div>
<h1 style="color: #fff; margin :0% 0% 0% 10%; padding:0% 0% 0% 10%;">上传文件</h1>
<form action="" method="post" enctype="multipart/form-data" style="color: #fff;
margin:margin :0% 0% 0% 20%; padding:0% 0% 0% 20%;">
报告:<input type="file" name="upload1"
style="background-color: #fff; color: #000;" />
<br/>
代码:<input type="file" name="upload2"
style="background-color: #fff; color: #000;"/>
<br/>
<div style="margin :0% 0% 0% 20%; padding:0% 0% 0% 24.5%;">
<input type="submit" value="上传" name="submit" />
</div>
</form>
</div>
</div>
</div>
<?php
/**
* @author 大眼仔~旭
* @copyright 2022
*/
session_start();
//使用session全局变量必须先引用此函数
if($_SESSION['logged']){
//判断用户是否已经登录,如已经登录:此变量会保存用户名
if(@$_POST['submit'])
{
$dir = '202011020110';
if(!is_dir($dir))
判断文件夹是否存在,如果不存在就创建一个文件夹
{
mkdir($dir);
}
for($i=1;$i<=2;$i++)
{
$t=$_FILES['upload'."$i"];
$newname = date('YmdHis').'-'.rand(100,900).$t['name'];
编辑一个新的文件名,防止文件名重复
if(is_uploaded_file($t['tmp_name']))
{
判断文件是否成功上传
if(!empty($t['name']))
{
判断接收的文件名是否为空
$extName = array ('doc','docx','pdf','wps','php','zip');
符合条件的文件的后缀名
$tmp = explode('.',$t['name']);
字符串通过特定的字符串分割成数组的函数,将接收到的文件名转换成数组
$temp = array_pop($tmp);
将文件的后缀名弹出
if(in_array($temp,$extName))
{
判断文件是否符合规定
if( $t['size'] < 2000000 )
{
判断接收到的文件的大小
echo '文件未超限';
if(move_uploaded_file($t['tmp_name'],"$dir".'/'.$newname))
{
将接收到的文件从临时路径移动到规定的文件夹下
echo "<script>alert('文件上传成功');</script>";
}else{
echo "<script>alert('文件上传失败');</script>";
}
}
else
echo "<script>alert('文件超限');
history.back()</script>";
}
else
echo "<script>alert('文件类型不符合');
history.back()</script>";
}
else
echo "<script>alert('文件名为空');
history.back()</script>";
}
}
}
}else{
echo "<script>alert('用户未登录!请登录!');
history.back()</script>";
}
?>
</body>
</html>
修改密码html代码:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="大眼仔~旭" />
<title>无标题 1</title>
</head>
<body>
<div style=" background-image:url(7777.png); height: 480px; width: 1210px; padding:10% 10%">
<div style="margin :0% 0% 0% 20%;width:60%; padding:5% 0% 5% 10%; background: #00AA88;">
<h1 style="margin :0% 10%; color: #fff;">修改密码</h1>
<form action="toModifyPW.php" method="post" onsubmit="return InputCheck(this)" style="margin :0% 0% 0% 10%">
<table borde='2' width='70%' cellspacing='3' cellpading='3'
bgcolor='#abcdef' style="color: #000;">
<tr>
<td>
原密码
</td>
<td>
<input type="password" name="oldpw" id="oldpassword"/>
</td>
</tr>
<tr>
<td>
新密码
</td>
<td>
<input type="password" name="pw" id="password"/>
</td>
</tr>
<tr>
<td>
确认密码
</td>
<td>
<input type="password" name="cpw" id="conpassword"/>
</td>
</tr>
</table>
<div style="margin :0% 0% 0% 20%; padding:0% 0% 0% 39.5%;">
<input type="submit" value="修改密码"/>
</div>
</form>
</div>
</div>
</body>
<script>
function InputCheck(temp){
if(temp.oldpassword.value==''){
alert('原密码不能为空');
temp.oldpassword.focus();
return false;
}
if(temp.password.value==''||temp.conpassword.value==''){
alert('新密码不能为空');
temp.password.focus();
return false;
}
if(temp.password.value!=temp.conpassword.value){
alert('两次密码不一致');
temp.password.focus();
return false;
}
return ture;
}
</script>
</html>
修改密码php代码:
<?php
/**
* @author 大眼仔~旭
* @copyright 2022
*/
session_start();
$username=$_SESSION['logged'];
if($username){
$oldpassword = md5(trim($_POST['oldpw']));
$password = md5(trim($_POST['pw']));
include_once('connect.php');
$sqlselect = "select * from userinfo where password='$oldpassword'";
$rselect = mysqli_query($conn,$sqlselect);
$numselect = mysqli_num_rows($rselect);
if($numselect){
$sqlupdate = "update userinfo set password='$password' where username='$username'";
$result = mysqli_query($conn,$sqlupdate);
if($result){
echo "<script>alert('密码修改成功,请重新登录!');location.href='login.html';</script>";
}
else{
echo "<script>alert('密码修改失败');location.href='reg.htm';</script>";
}
}
else{
echo "<script>alert('原密码不正确,请重试!');history.back();</script>";
}
}
else{
echo "<script>alert('请先登录');location.href='login.html';</script>";
}
?>
退出操作php代码:
<?php
/**
* @author 大眼仔~旭
* @copyright 2022
*/
session_start();
//使用session必须引用此函数
if(isset($_SESSION['logged'])){
//使用iset()函数判断全局变量是否存在值/变量
session_unset();
//释放session的变量,unset是释放变量的函数,它前面加session说明释放的是session的变量
session_destroy();//销毁session的文件
}
header('location:reg.htm')
//php的跳转函数:跳转到括号里面的字符串网页!
?>