我的登录页面似乎通过任何登录字段输入将我重定向到index.php。这是出乎意料的,因为它应该执行检查。此外,如果登录字段为空,则会重定向到一个空白页面。这也不应该如此,因为我已经设置了一个条件来处理这个结果。参见
logininc.php
<?php
require_once("assets/configs/db_config.php");
$user=$_POST['user'];
$password=$_POST['password'];
if(isset($_POST['submit'])){
//To ensure that none of the fields are blank when submitting the form if
if($user != NULL || $password != NULL)
// if(isset($_POST['user']) && isset($_POST['password'])) old code
{
$user = stripslashes($user);
$password = stripslashes($password);
$user = mysqli_real_escape_string($user);
$password = mysqli_real_escape_string($password);
$sql="SELECT * FROM $test_db WHERE user='$user' and password='$password'";
$result=mysqli_query($sql);
$row=mysqli_fetch_array($result);
if($row['user'] == $user && $row['password'] == $password)
{
session_start();
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
$_SESSION['loggedin'] = "true";
header("location:index.php");
}
else
{
echo ('<div id="error">Computer says no.</div>');
}
echo ('<div id="error">Enter something!</div>');
}
}
login.php
<form id="login-form" method="post" action="logininc.php"> <fieldset>
<legend>Login </legend>
<p>Please enter your username and password to access the administrator's panel</p>
<label for="user"> <input type="text" name="user" placeholder="Type your username here" id="user" /></label>
<label for="password"> <input type="password" name="password" placeholder="Type your password here" id="password" /></label>
<label for="submit"> <input type="submit" class="btn btn-primary"name="submit" id="submit" value="Login" /> </label> </fieldset> </form>
请告知。
与mysql_
扩展不同,mysqli
扩展要求在过程样式中使用时将连接对象传递给所有函数。
将假定$con
的连接资源传递给第一个参数。
mysqli_query($con, $sql);
也在您的mysqli_real_escape_string($con, $string);
中
使用此选项更改代码并查看。。
$sql="SELECT count(*) FROM test_db WHERE user='$user' and password='$password'";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
if($row[0]==1)
{
session_start();
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
$_SESSION['loggedin'] = "true";
header("location:index.php");
}
还有一件事,我注意到了,您使用的是$test_db
而不是test_db
。因此,我将其更改为test_db
。
看看如何使用mysqli_fetch_array
您也应该检查类型,在这种情况下:
if($row['user'] == $user && $row['password'] == $password)
改为使用:
if($row['user'] === $user && $row['password'] === $password)
为什么当你留下空白文件时,它会重定向到一个空白页面?试试这个代码:
$a = ''; //string type
$b = array(); // array type
var_dump($a==$b); // you will get true;
var_dump($a===$b); // you will get false;
您必须在代码的开头启动会话,您不能在代码中的任何位置启动会话
像一样启动
<?php
session_start();
...
如果不存在$_POST['user']
或$_POST['password']
,此页面将再次报告错误你需要像一样更改代码
if(isset($_POST['user'])&&isset($_POST['password']))
{
//proceed for login check
}
else{
//error: all credentials not sent
}
如前所述,字段为空不会产生Null,而是一个空字符串。因此,请使用isset()和empty()进行检查。
也可以尝试
$row = $result->mysqli_fetch_array(MYSQLI_ASSOC);
你真的应该把你的密码弄乱。