如何实现PHP查询数据库,信息匹配成功,跳转页面?

我用phpmyadmin新建了一个userinf的表,新建2个字段分别为:username和userpassword,在里面添加了一些模拟用户的数据如图。
我想要实现的是username和userpassword的数据匹配成功,就跳转页面。
这段代码该怎么写?完整的

<?php
$localhost = 'localhost';//本地的基本是用localhost
$dbname = 'dbname';//数据库名
$user = 'user';//用户名
$pw = 'pw';//密码
$pdo = new PDO("mysql:host=".$localhost.";dbname=".$dbname."",$user,$pw); 
$pdo->query('set names utf8');
$username = $_POST['username'];
$userpassword = $_POST['userpassword'];
$sql = 'SELECT userpassword FROM `userinf` WHERE `username`="' . $username . '"';
$row = $pdo->query($sql)->fetch();//查询数据库
if($userpassword == $row['userpassword'])
{
@header("Location: http://网址");//配对成功,跳转
}
else
{
exit('不好意思,你的密码不对');
}
/*
就是这么的简单,但是一般的验证用户密码不是这样的,首先一般存数据库里面的密码都是加密的,通常的MD5加密
而且POST提交过来的数据也要过滤一下
*/
?>
<!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=utf-8" />
<title>用户登录验证</title>
</head>

<body>
<form method="POST">
用户名:<input name="username" type="text" /> | 密码:<input name="userpassword" type="password" /> | <input type="submit" value="提交">
</form>
</body>
</html>

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网