首页 分享 PHP+MySQL制作简单的用户注册登录界面(注释超详细~附源代码)

PHP+MySQL制作简单的用户注册登录界面(注释超详细~附源代码)

来源:花匠小妙招 时间:2024-12-03 18:25

成果

网站能实现判断账户信息是否合法,同时附带验证码验证登录。在用户输入正确的用户名与密码后会有登录成功的弹窗,若输入的账户不存在,则会跳转至注册页面。

be9763a682424fe5a4b8476a9b3c456d.png

 4719364cd26b4f28b99984132965aac6.png

 实现过程

项目文件分配:

1ce61647bac94dcdb03ffae4ebbea31d.png

1.首先创建login.html

实现的是用户登录的界面,可以自定义css美化页面,这里主要目的是功能的实现而非界面美观。

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>用户登录</title>

<link href=../css/login.css rel="stylesheet"/>

</head>

<body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">

<div class=box>

<div class=title>用户登录</div>

<form action="../login.php" method="post">

<table class=login>

<tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>

<tr><th>密码:</th><td><input type="password" name="password"/></td></tr>

<tr><th>验证码:</th><td><input type="text" name="captcha"/></td></tr>

<tr><th></th><td><img src="../code.php"/></td></tr>

<tr><th></th><td><input type="submit" value="登录"/><a href="register.php"><input type="button" value='前往注册'></a></td>

</tr>

</table>

</form>

</div>

</body>

</html>

2.创建login.php,将其与login.html关联

<?php

header('content-type:text/html;charset=utf-8');

require 'login_db_connect.php';

if (isset($_POST['username'])&&isset($_POST['password'])){

$captcha = isset($_POST['captcha']) ? trim($_POST['captcha']) : '';

session_start();

if(empty($_SESSION['captcha'])){

exit('验证码已经过期,请返回并刷新页面重试。');

}

$true_captcha = $_SESSION['captcha'];

unset($_SESSION['captcha']);

if(strtolower($captcha) !== strtolower($true_captcha)){

exit('您输入的验证码不正确!请返回并刷新页面重试。');

}

$username=$_POST["username"];

$pwd=$_POST["password"];

$sql="select id,username,password from user where username='$username' and password='$pwd';";

$result=mysqli_query($con, $sql);

$row=mysqli_num_rows($result);

if (!$row){

echo "<script>alert('账号不存在或密码错误,点击前往注册');location='./register.php'</script>";

}else{

session_start();

$_SESSION['username']=$_POST['username'];

echo "<script>alert('欢迎');location='./index.php'</script>";

}

}

require './view/login.html';

3.注册页面register.html

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>用户注册</title>

<link href=../css/login.css rel="stylesheet"/>

</head>

<body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">

<div class=box>

<div class=title>用户注册</div>

<form action="../register.php" method="post">

<table class=login>

<tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>

<tr><th>密码:</th><td><input type="password" name="pwd"/></td></tr>

<tr><th></th><td><input type="submit" value="注册"/></td>

</tr>

</table>

</form>

</div>

</body>

</html>

4.创建register.php,与register.html关联。

<?php

header('content-type:text/html;charset=utf-8');

require 'login_db_connect.php';

if (isset($_POST['username'])&&isset($_POST['pwd'])){

$user=$_POST["username"];

$pwd=$_POST["pwd"];

if ($user=='' || $pwd==''){

exit('账号或密码不能为空');

}else {

$sql="insert into user(username,password) values ('$user','$pwd');";

$select="select username from user where username='$user'";

$result=mysqli_query($con, $select);

$row=mysqli_num_rows($result);

if(!$row){

if (mysqli_query($con,$sql)){

echo "<script>alert('注册成功,请登录');location='./login.php'</script>";

}else{

echo "<script>alert('注册失败,请重新注册');location='./regsiter.php'</script>";

}

}else{

echo "<script>alert('该用户已经存在,请直接登录');location='./login.php'</script>";

}

}

}

require './view/register.html';

5.创建code.php,用于生成验证码

<?php

$img_w = 100;

$img_h = 30;

$img = imagecreatetruecolor($img_w, $img_h);

$bg_color = imagecolorallocate($img,0xcc,0xcc,0xcc);

imagefill($img,0,0,$bg_color);

$count = 4;

$charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';

$charset_len = strlen($charset)-1;

$code = '';

for($i=0; $i<$count; ++$i) {

$code .= $charset[mt_rand(0,$charset_len)];

}

session_start();

$_SESSION['captcha'] = $code;

$fontSize = 16;

$fontStyle = './fonts/SourceCodePro-Bold.ttf';

for($i=0; $i<$count; ++$i){

$fontColor = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,50),mt_rand(0,255));

imagettftext (

$img, //画布资源

$fontSize, //文字大小

mt_rand(0,20) - mt_rand(0,25), //随机设置文字倾斜角度

$fontSize*$i+20,mt_rand($img_h/2,$img_h), //随机设置文字坐标,并自动计算间距

$fontColor, //文字颜色

$fontStyle, //文字字体

$code[$i] //文字内容

);

}

for($i=0; $i<300; ++$i){

$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);

}

for($i=0; $i<10; ++$i){

$color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));

imageline($img,mt_rand(0,$img_w),0,mt_rand(0,$img_h*5),$img_h,$color);

}

header('Content-Type: image/gif');

imagegif($img);

6.创建login_db_connect.php,用于数据库连接

数据库连接方式有多种,例如PDO连接,有兴趣的朋友可以自行了解

<?php

header('Content-type:text/html;charset=utf8');

$con=mysqli_connect("localhost","root","Huawei@123","LMS");

if (mysqli_connect_errno($con))

{

echo "连接 MySQL 失败: " . mysqli_connect_error();

}

R

*注意:篇幅有限,具体详见源代码,仅供学习参考,转载请注明出处。感谢支持。

源代码下载

链接:https://pan.quark.cn/s/0c31932c8b16
提取码:f7vM

相关知识

网上花店系统的源代码 免费下载
基于PHP+MySQL的网上花卉鲜花销售购物网站
移动应用开发做登录界面(app手机登录界面)
登录界面UI设计指南,跟着大厂学设计!
Java毕业设计
鲜花销售系统(论文+源代码)
Java实现鲜花销售系统
无聊吗?我教你制作手工花束,超简单
用户登录
用户登录界面设计代码html

网址: PHP+MySQL制作简单的用户注册登录界面(注释超详细~附源代码) https://www.huajiangbk.com/newsview849489.html

所属分类:花卉
上一篇: 回春花牌减肥茶保健功能/成分原料
下一篇: 【流程图】注册登录

推荐分享