我的代碼檢查用戶名是否存在於數據庫中,但是如果密碼不正確或爲空,用戶仍然可以登錄(重定向到welcome.php)。我如何實現它,以便密碼必須正確以及用戶名?如何檢查用戶名和密碼是否與數據庫匹配
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST'){
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "logreg";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user = $conn->real_escape_string(htmlspecialchars(trim($_POST['username'])));
$query = "SELECT `username` FROM `users` WHERE `username` = '$user'";
$result = $conn->query($query);
if($result->num_rows > 0) {
header('Location:welcome.php');
die();
}
else $message = 'user does not exist';
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Log In</title>
<link type="text/css" rel="stylesheet" href="css/bootstrap.css"/>
<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css"/>
<link type="text/css" rel="stylesheet" href="css/bootstrap-theme.css"/>
<link type="text/css" rel="stylesheet" href="css/bootstrap-theme.min.css"/>
<link type="text/css" rel="stylesheet" href="css/styles.css"/>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
</head>
<body>
<div class="header">
<div class="body">
<div id="loginbox" class="mainbox col-md-6 col-md-offset-3 col-sm-8 col-sm-offset-2">
<div class="panel">
<div class="panel-heading">
<div class="panel-title"><h1>Sign In</h1></div>
<div style="float:right; font-size: 80%; position: relative; top:-10px"><a href="passreset.html">Forgot password?</a></div>
</div>
<div style="padding-top:30px" class="panel-body" >
<div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>
<form id="loginform" class="form-horizontal" role="form" action = "index.php" method = "post" enctype="multipart/form-data">
<h4><?php if(isset($message)) : ?>
<div class="error"><?php echo $message; ?></div>
<?php endif; ?></h4>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input id="username" type="text" class="form-control" name="username" value="" placeholder="username"> </div>
<div style="margin-bottom: 25px" class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
<input id="login-password" type="password" class="form-control" name="password" placeholder="password">
</div>
<div class="input-group">
<div class="checkbox">
<label>
<input id="login-remember" type="checkbox" name="remember" value="1"> Remember me
</label>
</div>
<div style="margin-top:10px" class="form-group">
<!-- Button -->
<div class="col-sm-12 controls">
<input type = "submit" value = "Log In"></a>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-12 control">
<div style="border-top: 1px solid#888; padding-top:15px; font-size:85%">
Don't have an account!
<a href="register.html" onClick="$('#loginbox').hide(); $('#signupbox').show()">
Sign Up Here
</a>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<div style="border-top: 1px solid #999; padding-top:20px" class="form-group">
</div>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
旁註:你不需要'htmlspecialchars'。你已經在使用'real_escape_string',這可能會產生不利影響。 – 2014-12-04 15:42:47
在PHP手冊中有一個FAQ條目,我建議檢查:[密碼散列](http://php.net/manual/en/faq.passwords.php)。 – 2014-12-04 15:44:26
如果您願意切換到PDO,請使用[像這樣的東西](https://github.com/halfer/php-tutorial-project/blob/rebase4/lib/common.php#L153)來檢查登錄憑據(或者你可以修改它以與MySQLi一起工作 - 只要確保你使用參數化)。 – halfer 2014-12-04 15:49:02