企业管理系统定制开发PHP+MySQL制作简单的用户注册登录界面(注释超详细~附源代码)

成果

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

 

 实现过程

项目文件分配:

1.首先创建login.html

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

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户登录</title>
  6. <link href=../css/login.css rel="stylesheet"/>
  7. </head>
  8. <body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">
  9. <div class=box>
  10. <div class=title>用户登录</div>
  11. <form action="../login.php" method="post">
  12. <table class=login>
  13. <tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>
  14. <tr><th>密码:</th><td><input type="password" name="password"/></td></tr>
  15. <tr><th>验证码:</th><td><input type="text" name="captcha"/></td></tr>
  16. <tr><th></th><td><img src="../code.php"/></td></tr>
  17. <tr><th></th><td><input type="submit" value="登录"/><a href="register.php"><input type="button" value='前往注册'></a></td>
  18. </tr>
  19. </table>
  20. </form>
  21. </div>
  22. </body>
  23. </html>

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

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. //登录界面
  4. require 'login_db_connect.php';//连接数据库
  5. //判断表单是否提交,用户名密码是否提交
  6. if (isset($_POST['username'])&&isset($_POST['password'])){//登录表单已提交
  7. //获取用户输入的验证码
  8. $captcha = isset($_POST['captcha']) ? trim($_POST['captcha']) : '';
  9. //获取Session中的验证码
  10. session_start();
  11. if(empty($_SESSION['captcha'])){ //如果Session中不存在验证码,则退出
  12. exit('验证码已经过期,请返回并刷新页面重试。');
  13. }
  14. //获取验证码并清除Session中的验证码
  15. $true_captcha = $_SESSION['captcha'];
  16. unset($_SESSION['captcha']); //限制验证码只能验证一次,防止重复利用
  17. //忽略字符串的大小写,进行比较
  18. if(strtolower($captcha) !== strtolower($true_captcha)){
  19. exit('您输入的验证码不正确!请返回并刷新页面重试。');
  20. }
  21. //验证码验证通过,继续判断用户名和密码
  22. //获取用户输入的用户名密码
  23. $username=$_POST["username"];
  24. $pwd=$_POST["password"];
  25. $sql="select id,username,password from user where username='$username' and password='$pwd';";
  26. $result=mysqli_query($con, $sql);//执行sql语句
  27. $row=mysqli_num_rows($result);//返回值条目
  28. if (!$row){//若返回条目不存在则证明该账号不存在或者密码输入错误
  29. echo "<script>alert('账号不存在或密码错误,点击前往注册');location='./register.php'</script>";
  30. //exit('账号或密码错误');
  31. }else{//存在返回条目证明用户账号密码匹配,进入主页面
  32. session_start();
  33. $_SESSION['username']=$_POST['username'];
  34. echo "<script>alert('欢迎');location='./index.php'</script>";
  35. }
  36. }
  37. require './view/login.html';

3.注册页面register.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>用户注册</title>
  6. <link href=../css/login.css rel="stylesheet"/>
  7. </head>
  8. <body background="./images/loginbg.png" style="background-size: 100% 100%;background-attachment: fixed;">
  9. <div class=box>
  10. <div class=title>用户注册</div>
  11. <form action="../register.php" method="post">
  12. <table class=login>
  13. <tr><th>用户名:</th><td><input type="text" name="username"/></td></tr>
  14. <tr><th>密码:</th><td><input type="password" name="pwd"/></td></tr>
  15. <tr><th></th><td><input type="submit" value="注册"/></td>
  16. </tr>
  17. </table>
  18. </form>
  19. </div>
  20. </body>
  21. </html>

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

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. //注册页面
  4. require 'login_db_connect.php';//连接数据库
  5. //判断表单是否提交,用户名密码是否提交
  6. if (isset($_POST['username'])&&isset($_POST['pwd'])){//登录表单已提交
  7. //获取用户输入的用户名密码
  8. $user=$_POST["username"];
  9. $pwd=$_POST["pwd"];
  10. //判断提交账号密码是否为空
  11. if ($user=='' || $pwd==''){
  12. exit('账号或密码不能为空');
  13. }else {
  14. $sql="insert into user(username,password) values ('$user','$pwd');";//添加账户sql语句
  15. $select="select username from user where username='$user'";
  16. $result=mysqli_query($con, $select);//执行sql语句
  17. $row=mysqli_num_rows($result);//返回记录数
  18. if(!$row){//记录数不存在则说明该账户没有被注册过
  19. if (mysqli_query($con,$sql)){//查询insert语句是否成功执行,成功将返回 TRUE。如果失败,则返回 FALSE。
  20. //跳转登录页面
  21. echo "<script>alert('注册成功,请登录');location='./login.php'</script>";
  22. }else{//失败则重新跳转注册页面
  23. echo "<script>alert('注册失败,请重新注册');location='./regsiter.php'</script>";
  24. }
  25. }else{//存在记录数则说明注册的用户已存在
  26. echo "<script>alert('该用户已经存在,请直接登录');location='./login.php'</script>";
  27. }
  28. }
  29. }
  30. require './view/register.html';

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

  1. <?php
  2. //用于生成验证码
  3. //创建验证码画布
  4. $img_w = 100; //验证码的宽度
  5. $img_h = 30; //验证码的高度
  6. $img = imagecreatetruecolor($img_w, $img_h); //创建画布
  7. $bg_color = imagecolorallocate($img,0xcc,0xcc,0xcc); //画布颜色
  8. imagefill($img,0,0,$bg_color); //背景色
  9. //生成验证码文本
  10. $count = 4; //验证码位数
  11. $charset = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'; //随机因子
  12. $charset_len = strlen($charset)-1; //计算随机因子长度(作为取出时的索引)
  13. $code = ''; //保存生成的验证码
  14. for($i=0; $i<$count; ++$i) {
  15. //通过索引取出字符,mt_rand()用于获取指定范围内的随机数
  16. $code .= $charset[mt_rand(0,$charset_len)];
  17. }
  18. //将生成的文本保存到Session中
  19. session_start();//启动session
  20. $_SESSION['captcha'] = $code;
  21. //在画布中绘制验证码文本
  22. $fontSize = 16; //文字大小
  23. $fontStyle = './fonts/SourceCodePro-Bold.ttf'; //字体样式
  24. //生成指定长度的验证码
  25. for($i=0; $i<$count; ++$i){
  26. //随机生成字体颜色
  27. $fontColor = imagecolorallocate($img,mt_rand(0,100),mt_rand(0,50),mt_rand(0,255));
  28. imagettftext (
  29. $img, //画布资源
  30. $fontSize, //文字大小
  31. mt_rand(0,20) - mt_rand(0,25), //随机设置文字倾斜角度
  32. $fontSize*$i+20,mt_rand($img_h/2,$img_h), //随机设置文字坐标,并自动计算间距
  33. $fontColor, //文字颜色
  34. $fontStyle, //文字字体
  35. $code[$i] //文字内容
  36. );
  37. }
  38. //为验证码图片生成彩色噪点
  39. for($i=0; $i<300; ++$i){
  40. //随机生成颜色
  41. $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  42. //随机绘制干扰点
  43. imagesetpixel($img,mt_rand(0,$img_w),mt_rand(0,$img_h),$color);
  44. }
  45. //绘制干扰线
  46. for($i=0; $i<10; ++$i){
  47. //随机生成干扰线颜色
  48. $color = imagecolorallocate($img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
  49. //随机绘制干扰线
  50. imageline($img,mt_rand(0,$img_w),0,mt_rand(0,$img_h*5),$img_h,$color);
  51. }
  52. header('Content-Type: image/gif'); //输出图像
  53. imagegif($img);

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

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

  1. <?php
  2. //用于登录界面数据库连接
  3. //设置字符集
  4. header('Content-type:text/html;charset=utf8');
  5. //连接数据库
  6. $con=mysqli_connect("localhost","root","Huawei@123","LMS");//此处root为登录mysql的用户名,其后依此是密码与数据库名称
  7. if (mysqli_connect_errno($con))
  8. {
  9. echo "连接 MySQL 失败: " . mysqli_connect_error();
  10. }
  11. R

*注意:仅供学习参考,转载请注明出处。感谢支持。

源代码下载

https://url79.ctfile.com/d/33928079-50033272-dfd303?p=7430 (访问密码:7430)

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发