php进入数据库验证用户 php连接数据库实现用户登录功能( 二 )


如果是空就alert不能为空,然后return false;相反则return true;
而接收的PHP也要验证是否为空,如果严谨点还要对提交的数据进行过滤,防止sql注入 。
然后php再根据提交的数据搜MYSQL , 如果用户名和密码都相同时,echo 登录成功 , 相反则登录失败.
html
script
function check(obj){
with(obj){
if((user.value+"").length = 0){
alert("用户名不能为空");
return false;
}else if((pwd.value+"").length = 0){
alert("用户名不能为空");
return false;
}else{
return true;
}
}
}
/script
body
form action="check.php" method="post" onsubmit="return check(this)"
input type="text" name="user" value=""
input type="password" name="pwd" value=""
input type="submit" name="submit" value="https://www.04ip.com/post/登录"
input type="cancel" name="cancel" value="https://www.04ip.com/post/取消"
/form
/body
/html
?php
$conn = mysql_connect( "数据库地址", "数据库用户名", "密码" );
mysql_query("set names utf8");
mysql_select_db( "数据库名" );
function inject_check($sql_str){
return preg_match("/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile|%|eval|=|and|'|;|exec|count/i", $sql_str);// 进行过滤
}
if(!empty($_POST)){
foreach($_POST as $key = $value){
if(inject_check($value)){
exit ('scriptalert("地址栏输入发现有非法字符,请重新输入!");history.go(-1);/script');
die ();
}
}
}
$res = mysql_query("SELECT count(*) as m from `表名` where 用户名='${_POST['user']}' AND 密码='${_POST['pwd']}'");
$row = mysql_fetch_object($res);
if($row-m 0){
echo "登陆成功";
}else{
echo "用户名或密码错误";
}
exit;
?
关于php进入数据库验证用户和php连接数据库实现用户登录功能的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息 , 记得收藏关注本站 。

推荐阅读