0
我一直在看我的代码几天,但似乎无法找到问题。我是PHP新手,所以我并不十分熟悉它。无法执行登录功能。没有错误
以下是我的代码。没有错误。没有注册的会话变量值。
DB-config.php文件
<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'mcsh';
$conn = mysqli_connect($host, $user, $pass, $db);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
?>
的login.php
<form id="user-login" action="index.php" method="POST">
<h1>Administrator Login</h1>
<input type="text" name="username" placeholder="Username" required/>
<input type="password" name="password" placeholder="Password" required/>
<button type="submit">Login</button>
<a href="/">Forgot your password?</a>
</form>
<?php
if (!empty($_POST)) {
if (!empty($_SESSION['username'])) {
header("Location: index.php");
}
$username = $_POST['username'];
$password = $_POST['password'];
include("../config/db-config.php");
$sql = "SELECT `userid`, `password` FROM users WHERE userid = '" . $username . "' AND userlevel = '99'";
$result = mysqli_query($conn, $sql);
if ($row = mysqli_fetch_assoc($result)) {
if (password_verify($password, $row['password'])) {
$_SESSION['username'] = $row['userid'];
header("Location: index.php");
exit;
}
else {
?>
<p class="msg" id="error">Invalid username or password. Please try again.</p>
<?php
}
}
else {
?>
<p class="msg" id="error">Invalid username or password. Please try again.</p>
<?php
}
}
?>
的index.php
<?php
session_start();
include("../config/config.php");
if (empty($_SESSION['username'])) {
header("Location: login.php");
}
else {
//the rest of the index page...
?>
**警告**:使用'mysqli'时,您应该使用[参数化查询](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param' ](http://php.net/manual/en/mysqli-stmt.bind-param.php)将用户数据添加到您的查询。 **不要**使用字符串插值或连接来完成此操作,因为您创建了严重的[SQL注入漏洞](http://bobby-tables.com/)。 **绝不**将'$ _POST'或'$ _GET'数据直接放入查询中,如果有人试图利用您的错误,这会非常有害。 – tadman
**警告**:编写您自己的访问控制层并不容易,并且有很多机会使其严重错误。这有很多危险的[SQL注入漏洞](http://bobby-tables.com/),因为你没有[妥善转义值](http://bobby-tables.com/php)。此代码允许*任何人*从您的网站获得*任何*。 **不要**写你自己的认证系统。任何[开发框架](http://codegeekz.com/best-php-frameworks-for-developers/)像[Laravel](http://laravel.com/)都带有一个[认证系统](https:/ /laravel.com/docs/5.2/authentication)内置。 – tadman
感谢您的警告。我知道这个登录功能是非常不安全的。我唯一的问题是它没有做任何事情。不给我错误或这样的。 – devgirl