php学习-实战项目

登录注册

login_db_connect.php

连接数据库

<?php
//用于登录界面数据库连接
//设置字符集
header('Content-type:text/html;charset=utf8');

//连接数据库
$con=mysqli_connect("localhost","root","root","lms");
if (mysqli_connect_errno($con))
{
    echo "连接 MySQL 失败: " . mysqli_connect_error();
} 

login.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="../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="submit" value="登录"/><a href="register.php"><input type="button" value='前往注册'></a></td>
						
			</tr>
			
		</table>
	
	</form>
</div>
</body>
</html>

login.php

<?php
header('content-type:text/html;charset=utf-8');
//登录界面
require 'login_db_connect.php';//连接数据库


//判断表单是否提交,用户名密码是否提交
if (isset($_POST['username'])&&isset($_POST['password'])){//登录表单已提交
    

    //获取用户输入的用户名密码
    $username=$_POST["username"];
    $pwd=$_POST["password"];
    $sql="select id,username,password from user where username='$username' and password='$pwd';";
    $result=mysqli_query($con, $sql);//执行sql语句
    $row=mysqli_num_rows($result);//返回值条目
    if (!$row){//若返回条目不存在则证明该账号不存在或者密码输入错误
        echo "<script>alert('账号不存在或密码错误,点击前往注册');location='./register.php'</script>";
        //exit('账号或密码错误');
    }else{//存在返回条目证明用户账号密码匹配,进入主页面
        session_start();
        $_SESSION['username']=$_POST['username'];
        echo "<script>alert('欢迎');location='./index.php'</script>";
    }   
}


require './view/login.html';

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>

register.php

<?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');";//添加账户sql语句
        $select="select username from user where username='$user'";
        $result=mysqli_query($con, $select);//执行sql语句
        $row=mysqli_num_rows($result);//返回记录数
        if(!$row){//记录数不存在则说明该账户没有被注册过
            if (mysqli_query($con,$sql)){//查询insert语句是否成功执行,成功将返回 TRUE。如果失败,则返回 FALSE。
                //跳转登录页面
                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';

相关推荐

  1. Django学习(2)项目实战

    2024-01-24 08:40:01       26 阅读
  2. <span style='color:red;'>php</span><span style='color:red;'>学习</span>

    php学习

    2024-01-24 08:40:01      42 阅读
  3. <span style='color:red;'>PHP</span><span style='color:red;'>学习</span>

    PHP学习

    2024-01-24 08:40:01      147 阅读
  4. vscode 调试 php项目

    2024-01-24 08:40:01       68 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-24 08:40:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-24 08:40:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-24 08:40:01       82 阅读
  4. Python语言-面向对象

    2024-01-24 08:40:01       91 阅读

热门阅读

  1. CGAL 网格连通聚类

    2024-01-24 08:40:01       50 阅读
  2. 06 栈

    06 栈

    2024-01-24 08:40:01      56 阅读
  3. oracle materialized views 是啥

    2024-01-24 08:40:01       59 阅读
  4. gtest 单元测试

    2024-01-24 08:40:01       56 阅读
  5. 汇编(1)

    2024-01-24 08:40:01       57 阅读
  6. OC-NSNumber

    2024-01-24 08:40:01       53 阅读
  7. opencv中的几个重要函数

    2024-01-24 08:40:01       56 阅读