用PHP制作简单的登录页面

创建了一个登录页面(1.php)包含用户名和密码输入框,表单提交到page2.php进行验证。如果用户名为'admin'且密码为'123456',则显示登录成功警告;否则,提示账号或密码错误并返回登录页面。
摘要由CSDN通过智能技术生成

题目要求:

登录页面:设计一个登录页面,获取登录表单中的用户名和密码,如果用户名为“admin”,密码为“123456”,则提示登录成功。效果参考下图:
在这里插入图片描述

代码:

1.php

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>表单</title>
		<style>
			.a{
				display: flex;
				flex-direction: column;
				align-items: center;
			}
			table {
				border:1px solid #000000;
		        text-align: center;
			}
			table th{
				background-color: darkgray;
				border:1px solid #000000;
			}  
			table td{
				border:1px solid #000000;
				text-align: center;
			}

		</style>
	</head>
	<body>
		<div class="a">
			<form action="page2.php" method="post">
				<table>
					<tr><th colspan="2">用户登录表单</th></tr>
					<tr>
						<td>用户名:</td>
						<td><input type="text" name="a" required></td>
					</tr>
					<tr>
						<td>密码:</td>
						<td><input type="password" name="b" required></td>
					</tr>
					<tr>
						<td colspan="2"><input type="submit" name="submit" value="登录"/><input type="reset" name=“Reset”></td>
					</tr>
				</table>
			</from>
		</div>
	</body>
</html>

page2.php

<?php
$username=$_POST["a"];
$password=$_POST["b"];
	if(strcmp($username,"admin")==0 && strcmp($password,"123456" )==0)
	{

		echo"<script>";
		echo"alert('登录成功');";
		echo"</script>";
	}else{
		echo"<script>";
		echo"alert('账号或密码错误');";
		echo "location.href='1.php';"; 
		echo"</script>";
	}
?>

成果图:

在这里插入图片描述

登录成功页面
在这里插入图片描述

  • 11
    点赞
  • 155
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的示例代码,可以用来制作登录注册页面。代码中使用了 MySQL 数据库来存储用户信息,需要在本地或者服务器上搭建好 PHP 和 MySQL 环境并创建好相应的数据库和表。 1. 注册页面代码(register.php) ```php <!DOCTYPE html> <html> <head> <title>注册页面</title> </head> <body> <h2>注册</h2> <form method="post" action="register.php"> <label>用户名:</label> <input type="text" name="username" required><br><br> <label>密码:</label> <input type="password" name="password" required><br><br> <label>确认密码:</label> <input type="password" name="confirm_password" required><br><br> <input type="submit" name="register" value="注册"> </form> <?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "password", "test_db"); // 处理用户提交的注册信息 if (isset($_POST['register'])) { $username = mysqli_real_escape_string($conn, $_POST['username']); $password = mysqli_real_escape_string($conn, $_POST['password']); $confirm_password = mysqli_real_escape_string($conn, $_POST['confirm_password']); // 判断密码是否一致 if ($password != $confirm_password) { echo "两次输入的密码不一致!"; } else { // 判断用户名是否已存在 $sql = "SELECT * FROM users WHERE username='$username'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { echo "用户名已存在!"; } else { // 将用户信息插入数据库 $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; if (mysqli_query($conn, $sql)) { echo "注册成功!"; } else { echo "注册失败:" . mysqli_error($conn); } } } } // 关闭数据库连接 mysqli_close($conn); ?> </body> </html> ``` 2. 登录页面代码(login.php) ```php <!DOCTYPE html> <html> <head> <title>登录页面</title> </head> <body> <h2>登录</h2> <form method="post" action="login.php"> <label>用户名:</label> <input type="text" name="username" required><br><br> <label>密码:</label> <input type="password" name="password" required><br><br> <input type="submit" name="login" value="登录"> </form> <?php // 连接数据库 $conn = mysqli_connect("localhost", "root", "password", "test_db"); // 处理用户提交的登录信息 if (isset($_POST['login'])) { $username = mysqli_real_escape_string($conn, $_POST['username']); $password = mysqli_real_escape_string($conn, $_POST['password']); // 查询数据库中是否存在该用户 $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { echo "登录成功!"; } else { echo "用户名或密码错误!"; } } // 关闭数据库连接 mysqli_close($conn); ?> </body> </html> ``` 注意事项: - 数据库连接参数需要根据实际情况修改; - 数据库中需要创建一个名为 `users` 的表,包含 `id`、`username`、`password` 三个字段,其中 `id` 为自增主键; - 以上代码仅为示例,没有进行输入合法性验证、用户信息加密等安全性处理。在实际开发中需要进行更完善的处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值