php+ajax登录验证代码
web上会经常遇到登录的情况,一个无刷新的ajax登录方式会给浏览者带来更好的体验。这个代码写了很久了,是上次没做完的一个系统的一部分。由于不是 靠web编程吃饭,php仅仅是个爱好,所以我会经常做些“半截”东西,细想起来真是没有做完一个完整的东西,都是把基本功能实现就没兴趣,就放弃了!有 些代码还是很有用,所以记下来以备将来有用!
这个登录验证需要用到3个文件:index.html登录界面,ajax.js基本的ajax代码,login.php操作数据库文件。当然还有css样式文件,不影响使用就不发了。
下面是index.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录</title>
<script src="ajax.js"></script>
<script src="md5.js"></script>
<script src="function.js"></script>
<script language="javascript">
var login=new Ajax();
login.callback=function(json){
if(json.msg!='AAA'){
alert (json.msg);
}else{
window.location=json.page;
}
}
function send(){
var ul='a=tec&name='+document.getElementById('name').value+'&password='+hex_md5(document.getElementById('pass').value)+'&yzm='+document.getElementById('yzm').value;
login.send(ul,'login.php');
}
</script>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="login">
<div align="center">管理登录 </div>
<form id="form" name="form" method="post" onsubmit="return checkfm(form)" action="javascript:send()">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="27%" height="30"><div align="right">用户名</div></td>
<td colspan="2"><input name="name" type="text" id="name" size="20" maxlength="20" /></td>
</tr>
<tr>
<td height="30"><div align="right">密 码</div></td>
<td colspan="2"><label>
<input name="pass" type="password" id="pass" value="" size="20" />
</label></td>
</tr>
<tr>
<td height="30"><div align="right">验证码</div></td>
<td width="13%"><input name="yzm" type="text" id="yzm" size="6" maxlength="4" /></td>
<td width="60%"><img src="yzm.php" id="Image1" onclick="Image1.src='./yzm.php?'+Math.random()" alt="点击刷新" /></td>
</tr>
</table>
<div align="center">
<input type="submit" name="Submit" value="登录" />
<input type="reset" name="Submit2" value="重置" />
</div>
</form>
</div>
</body>
</html>
ajax.js
function Ajax(){
var _this=this
var getXmlHttp=function(){
var http_request = false;
if (window.XMLHttpRequest){
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType){
http_request.overrideMimeType('text/xml');
}
}else if (window.ActiveXObject){
try{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e){
try{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e)
{}
}
}
if (!http_request){
alert('Giving up <img src="http://fourier.eblhost.cn/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley"> Cannot create an XMLHTTP instance');
return false;
}
return http_request;
}
_this.send=function(ul,page){
df = getXmlHttp();
df.onreadystatechange = onChange;
df.open("post",page,true);
df.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
df.send(ul);
}
_this.callback=function(json){
alert(json);
}
var onChange=function(){
if(df.readyState == 4 ){
if(df.status == 200) {
json = eval('(' + df.responseText + ')');
_this.callback(json);
}
}
}
}
login.php
include_once('db.php');
session_start();
//首先验证验证码的正确性
if(isset($_POST['yzm']) and strtolower($_POST['yzm'])==strtolower(trim($_SESSION['yzm']))){
if(isset($_POST['a']) and $_POST['a']=='tec'){
$name=$_POST['name'];
$password=$_POST['password'];
try {
$dbh = new PDO($dsn);
$sql="select * from user where name='".$name."'";
$sth = $dbh->query($sql)->fetch(); $dsn= null;
if ($sth==""){ $arr = array ('msg'=>'该用户不存在!');
}else{ if($password==md5($sth['password'])){
$_SESSION['t_name']=$name;
$_SESSION['t_id']=$sth['id'];
$arr = array ('msg'=>'AAA','page'=>'admin.php');
}else{ $arr = array ('msg'=>'密码错误!');
}
}
} catch (PDOException $e) {
$arr = array ('msg'=>'连接数据库失败!');
$dsn=null;
}
}
}else{
//验证码错误则返回错误信息
$arr = array ('msg'=>'验证码错误!');
}
Published by 阿飞 on
Tags: none
暂无评论