布尔值 SQL 和 PHP 验证
Booleans SQL and PHP validation
我在 table(用户)中有一列名为 admin,其数据类型为布尔值。两个用户设置为"true"
我的 objective 是当这两个登录时,他们可以访问后台,但到目前为止代码无法正常工作:
<?php
session_start();
$error="";
$successMessage="";
if ($_POST){
if(!isset($_POST["salada"]) || $_POST["salada"]===""){
$error = "PHP: An email is required <br>";
}
if(!isset($_POST["arroz"]) || $_POST["arroz"]===""){
$error .= "PHP: A password is required";
}
if ($error !=""){
$error = '<div class="error-login">'.$error.'</div>';
}else {
require("MGconfig.php");
$email = mysqli_real_escape_string($connection, $_POST["salada"]);
$pwd = md5(mysqli_real_escape_string($connection, $_POST["arroz"]));
$result = mysqli_query($connection, "select name, id from users where email = '".$email."' and password = '".$pwd."'");
if (mysqli_num_rows($result) !==1){
$error='<div class="error-login">PHP: Invalid email or password</div>';
header("Location:index.php?error=".$error);
}else {
$nome = mysqli_fetch_row($result);
$_SESSION["name"] = $nome[0];
$_SESSION["id"]=$nome[1];
header ("Location: main.php");
}
}
}
?>
<?php
if($_SESSION['admin'] !=0){
header ("Location:admin.php");
}?>
有人能告诉我为什么不起作用吗?是语法吗?如果我比较字段 "name",则限制有效...提前致谢!
问题是,您还没有 selected SELECT
查询中的 admin
列,您只有 selected id
和 name
列。另外,您无法在任何地方检查登录用户是否为管理员。
所以解决方案是,select SELECT
查询中的 admin
列,并使用该列值来检查登录用户是否为管理员,例如这个:
// your code
$result = mysqli_query($connection, "select name, id, admin from users where email = '".$email."' and password = '".$pwd."'");
if (mysqli_num_rows($result) !== 1){
$error='<div class="error-login">PHP: Invalid email or password</div>';
header("Location:index.php?error=".$error);
}else{
$nome = mysqli_fetch_row($result);
$_SESSION["name"] = $nome[0];
$_SESSION["id"] = $nome[1];
if($nome[2]){
// logged in user is admin
$_SESSION["admin"] = true;
}else{
// logged in user is not admin
$_SESSION["admin"] = false;
}
header ("Location: main.php");
exit();
}
// your code
旁注:了解prepared statements because right now your query is susceptible to SQL injection. Also see how you can prevent SQL injection in PHP。
我在 table(用户)中有一列名为 admin,其数据类型为布尔值。两个用户设置为"true"
我的 objective 是当这两个登录时,他们可以访问后台,但到目前为止代码无法正常工作:
<?php
session_start();
$error="";
$successMessage="";
if ($_POST){
if(!isset($_POST["salada"]) || $_POST["salada"]===""){
$error = "PHP: An email is required <br>";
}
if(!isset($_POST["arroz"]) || $_POST["arroz"]===""){
$error .= "PHP: A password is required";
}
if ($error !=""){
$error = '<div class="error-login">'.$error.'</div>';
}else {
require("MGconfig.php");
$email = mysqli_real_escape_string($connection, $_POST["salada"]);
$pwd = md5(mysqli_real_escape_string($connection, $_POST["arroz"]));
$result = mysqli_query($connection, "select name, id from users where email = '".$email."' and password = '".$pwd."'");
if (mysqli_num_rows($result) !==1){
$error='<div class="error-login">PHP: Invalid email or password</div>';
header("Location:index.php?error=".$error);
}else {
$nome = mysqli_fetch_row($result);
$_SESSION["name"] = $nome[0];
$_SESSION["id"]=$nome[1];
header ("Location: main.php");
}
}
}
?>
<?php
if($_SESSION['admin'] !=0){
header ("Location:admin.php");
}?>
有人能告诉我为什么不起作用吗?是语法吗?如果我比较字段 "name",则限制有效...提前致谢!
问题是,您还没有 selected SELECT
查询中的 admin
列,您只有 selected id
和 name
列。另外,您无法在任何地方检查登录用户是否为管理员。
所以解决方案是,select SELECT
查询中的 admin
列,并使用该列值来检查登录用户是否为管理员,例如这个:
// your code
$result = mysqli_query($connection, "select name, id, admin from users where email = '".$email."' and password = '".$pwd."'");
if (mysqli_num_rows($result) !== 1){
$error='<div class="error-login">PHP: Invalid email or password</div>';
header("Location:index.php?error=".$error);
}else{
$nome = mysqli_fetch_row($result);
$_SESSION["name"] = $nome[0];
$_SESSION["id"] = $nome[1];
if($nome[2]){
// logged in user is admin
$_SESSION["admin"] = true;
}else{
// logged in user is not admin
$_SESSION["admin"] = false;
}
header ("Location: main.php");
exit();
}
// your code
旁注:了解prepared statements because right now your query is susceptible to SQL injection. Also see how you can prevent SQL injection in PHP。