展开全部
第一次学PHP就是62616964757a686964616fe58685e5aeb931333332616533做这个验证..
html做个表单,
当表单οnsubmit=return check();调用自写js来判断用户名和密码是否为空,
如果是空就alert不能为空,然后return false;相反则return true;
而接收的PHP也要验证是否为空,如果严谨点还要对提交的数据进行过滤,防止sql注入。
然后php再根据提交的数据搜MYSQL,如果用户名和密码都相同时,echo 登录成功,相反则登录失败.
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;
}
}
}
$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 ('');
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;
?>