帮我优化这段代码,使其可以成功实现管理员和普通用户登录<?php //数据库连接 require_once 'connect.php'; //从登录页接受来的数据 $name=$_POST['username']; $pwd=$_POST['password']; session_start(); $_SESSION['username'] = $username; $_SESSION['user_type'] = $row['user_type']; $sql="select id,username,password from user where username='$name' AND password='$pwd';"; $result=mysqli_query($conn,$sql); $row=mysqli_num_rows($result); if(!$row){ echo "<script>alert('密码错误,请重新输入');location='login.html'</script>"; } else{ // 跳转到相应的页面 if ($row['user_type'] == 'admin') { header('Location:zsgc.php'); } else { header("refresh:3;url='welcome.php'"); } }
时间: 2023-08-31 10:42:47 浏览: 54
这段代码有一些问题,下面是改进的建议:
1. 首先,需要在接受到数据之前对数据进行安全过滤,避免 SQL 注入攻击。可以使用 mysqli_real_escape_string 函数来实现。
2. 在判断用户是否存在时,应该使用 mysqli_fetch_assoc 函数来获取查询结果。如果查询结果为空,则返回错误信息。
3. 在设置 session 变量时,应该使用正确的变量名,即 $name 而不是 $username。
4. 在跳转页面时,应该使用 exit 函数来避免后续代码的执行。
下面是代码的改进版本:
```
<?php
// 数据库连接
require_once 'connect.php';
// 从登录页接受来的数据
$name = mysqli_real_escape_string($conn, $_POST['username']);
$pwd = mysqli_real_escape_string($conn, $_POST['password']);
// 查询用户是否存在
$sql = "SELECT id, username, user_type FROM user WHERE username='$name' AND password='$pwd';";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
if (!$row) {
// 错误处理
echo "<script>alert('密码错误,请重新输入');location='login.html'</script>";
} else {
// 设置 session 变量
session_start();
$_SESSION['username'] = $name;
$_SESSION['user_type'] = $row['user_type'];
// 跳转到相应的页面
if ($row['user_type'] == 'admin') {
header('Location:zsgc.php');
exit();
} else {
header("refresh:3;url='welcome.php'");
exit();
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE html> <?php require_once('MkEncrypt.php'); MkEncrypt('1234'); //这个就是密码 ?> <html> <head> <meta charset="UTF-8"> <title>GOLA 2.0</title> </head> <style type="text/css"> .wrapper {width: 1000px;margin: 20px auto;} h2 {text-align: center;} .add {margin-bottom: 20px;} .add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;} td {text-align: center;} </style> <body> GOLA 2.0 ID 用户呀 手机呀 刷新密码 总资 可用 操作 <?php // 1.导入配置文件 require "dbconfig.php"; // 2. 连接mysql $link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!"); // 选择数据库 mysql_select_db(DBNAME,$link); // 编码设置 mysql_set_charset('utf8',$link); // 3. 从DBNAME中查询到user数据库,返回数据库结果集,并按照addtime降序排列 $sql = 'select * from user order by id asc'; // 结果集 $result = mysql_query($sql,$link); // var_dump($result);die; // 解析结果集,$row为新闻所有数据,$userNum为新闻数目 $userNum=mysql_num_rows($result); for($i=0; $i<$userNum; $i++){ $row = mysql_fetch_assoc($result); echo ""; echo "{$row['id']} "; echo "{$row['agent_name']} "; echo "{$row['phone']} "; echo "{$row['with_pwd']} "; echo "{$row['user_amt']} "; echo "{$row['enable_amt']} "; echo " 删除 修改 "; echo " "; } // 5. 释放结果集 mysql_free_result($result); mysql_close($link); ?> <script type="text/javascript"> function del (id) { if (confirm("确定删除这条新闻吗?")){ window.location = "action-del.php?id="+id; } } </script> </body> </html> 美化一下页面
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
找出这串代码的错误并给出正确答案<!DOCTYPE html> <?php require_once('./conn.php'); $sql = "select * from result"; $result = $conn->query($sql); if ($result->num_rows>0){ while($row = $result->fetch_assoc()) { ?> <?php echo $row['id'] ?> <?php echo $row['name'] ?> <?php echo $row['age'] ?> <?php echo $row['result'] ?> <button onclick="toUpdate(this)">修改</button> <button onclick="remove(this)">删除</button> <?php } } $conn->close(); ?> <html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="js/index.js"></script> <title>学生成绩管理系统</title> </head> <body> 学生成绩管理系统 <button>添加</button> </body> </html> <script type="text/javascript"> function remove(ele){ let id = ele.parentElement.parentElement.children[0].innerText; window.location.href="remove_server.php?id="+id; } function toUpdate(ele) { let id = ele.parentElement.parentElement.children[0].innerText; window.location.href = "./update.php?id="+id; } </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>学生成绩管理系统</title> //<script type="text/javascript" src="js/script.js"></script> </head> <body> 学生成绩管理系统 <button>添加</button> <?php require_once('./server/conn.php'); $sql = "select * from result"; $result = $conn->query($sql); if ($result->num_rows > 0) { //输出数据 while ($row = $result->fetch_assoc()) { ?> <?php echo $row['id'] ?> <?php echo $row['name'] ?> <?php echo $row['age'] ?> <?php echo $row['result'] ?> <button onclick = "toUpdata(this) ">修改</button> <button onclick = "remove(this)">删除</button> <?php } } $conn->close(); ?> </body> </html> 请给我找出错误
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
找出这段代码的错误并给出正确答案<html> <head> <meta charset="utf-8" /> <script type="text/javascript" src="js/index.js"></script> <title>学生成绩管理系统</title> </head> <body> 学生成绩管理系统 <button>添加</button> </body> </html> <?php result_once('./server.conn.php'); $sql = "select * from result"; $result = $conn->query($sql); if ($result->num_rows > 0) { //输出数据 while($row = $result->fetch_assoc()) { ?> <?php echo $row['id'] ?> <?php echo $row['name'] ?> <?php echo $row['age'] ?> <?php echo $row['result'] ?> <button onclick="toUpdate(this)">修改</button> <button onclick="remove(this)">删除</button> <?php } } $conn->close(); ?> <script type="text/jscript"> function remove(ele){ //删除成绩 let id = ele.parentElement.parentElement.children[0].innerText; window.location.href="./server/remove_server.php?id=" + id; } function toUpdate(ele){ //跳转到成绩修改页面 let id = ele.parentElement.parentElement.children[0].innerText; window.location.href="./update.php?id=" + id; } </script>
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)