这是我的index.php:
<?php
session_start();
//DB configuration Constants
define('_HOST_NAME_', 'localhost');
define('_USER_NAME_', 'root');
define('_DB_PASSWORD', '');
define('_DATABASE_NAME_', 'myblog');
//PDO Database Connection
try {
$databaseConnection = new PDO('mysql:host='._HOST_NAME_.';dbname='._DATABASE_NAME_, _USER_NAME_, _DB_PASSWORD);
$databaseConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
if(isset($_POST['submit'])){
$errMsg = '';
//username and password sent from Form
$username = trim($_POST['username']);
$password = trim($_POST['password']);
if($username == '')
$errMsg .= 'You must enter your Username<br>';
if($password == '')
$errMsg .= 'You must enter your Password<br>';
if($errMsg == ''){
$records = $databaseConnection->prepare('SELECT id,username,password FROM tbl_users WHERE username = :username');
$records->bindParam(':username', $username);
$records->execute();
$results = $records->fetch(PDO::FETCH_ASSOC);
if(count($results) > 0 && password_verify($password, $results['password'])){
$_SESSION['username'] = $results['username'];
header('location:dashboard.php');
exit;
}else{
$errMsg .= 'Username and Password are not found<br>';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login Page PHP Script</title>
<style type="text/css">
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
label
{
font-weight:bold;
width:100px;
font-size:14px;
}
.box
{
border:1px solid #006D9C;
margin-left:10px;
width:60%;
}
.submit{
border:1px solid #006D9C;
background-color:#006D9C;
color:#FFFFFF;
float:right;
padding:2px;
}
</style>
</head>
<body bgcolor="#FFFFFF">
<div align="center">
<div class="tLink"><strong>Tutorial Link:</strong> <a href="http://www.stepblogging.com/get-alexa-rank-using-php/">Click Here</a></div><br />
<div class="tLink"><strong>Demo Login Detail:</strong> demo / demo </div><br />
<div style="width:300px; border: solid 1px #006D9C; " align="left">
<?php
if(isset($errMsg)){
echo '<div style="color:#FF0000;text-align:center;font-size:12px;">'.$errMsg.'</div>';
}
?>
<div style="background-color:#006D9C; color:#FFFFFF; padding:3px;"><b>Login</b></div>
<div style="margin:30px">
<form action="" method="post">
<label>Username :</label><input type="text" name="username" class="box"/><br /><br />
<label>Password :</label><input type="password" name="password" class="box" /><br/><br />
<input type="submit" name='submit' value="Submit" class='submit'/><br />
</form>
</div>
</div>
</div>
</body>
</html>
这是logout.php:
<?php
session_start();
session_destroy();
header("location:index.php");
exit;
?>
最后是dashboard.php:
?php
session_start();
echo 'Welcome '.$_SESSION['username'];
?>
<br /><a href='logout.php'>Logout</a>
当我输入输入详细信息并提交时...显示找不到用户名和密码。
我创建了数据库并插入sql查询很好..
我可以知道,我的错误是什么?
谢谢,
答案 0 :(得分:0)
请试试这个<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
我想你错过了这个
答案 1 :(得分:0)
在这一行[{1}}中,您使用的是if(count($results) > 0 && password_verify($password, $results['password'])){
我看到两件事。
首先 - 你的哈希在哪里?
第二 - 这条线本身不正确。
应该是...... password_verify
但是......在使用它之前,你必须得到该记录的哈希值。最有可能在创建用户时存储在哈希表中。并定义像这样的变量...... if(count($results) > 0 && password_verify($password, $hash)){
如果您没有哈希密码,请将您的sql语句更改为$hash = $row['hash']
。然后为密码创建另一个参数绑定。
注意:正确绑定应如下所示:
'SELECT id,username,password FROM admin WHERE username=:username AND password=:password'
在$records->bindValue(':id', $id, PDO::PARAM_INT); // for integers
$records->bindValue(':username', $username, PDO::PARAM_STR); // for strings and dates
下直接使用$totalRows = $records->rowCount();
来获取行数也是一种很好的做法。而不是$results = $records->fetch(PDO::FETCH_ASSOC);
它将是if(count($results) && ...
这样你就定义了一个变量,如果需要的话,它总是可以在页面的其他地方使用。
快乐的编码!
<强>参考强>