php+ajax登录验证代码

    web上会经常遇到登录的情况,一个无刷新的ajax登录方式会给浏览者带来更好的体验。这个代码写了很久了,是上次没做完的一个系统的一部分。由于不是 靠web编程吃饭,php仅仅是个爱好,所以我会经常做些“半截”东西,细想起来真是没有做完一个完整的东西,都是把基本功能实现就没兴趣,就放弃了!有 些代码还是很有用,所以记下来以备将来有用!
    这个登录验证需要用到3个文件:index.html登录界面,ajax.js基本的ajax代码,login.php操作数据库文件。当然还有css样式文件,不影响使用就不发了。

下面是index.html


<html>
<head>
<meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=utf-8&#34; />
<title>登录</title>
<script src=&#34;ajax.js&#34;></script>
<script src=&#34;md5.js&#34;></script>
<script src=&#34;function.js&#34;></script>
<script language=&#34;javascript&#34;>
var login=new Ajax();
login.callback=function(json){
if(json.msg!=&#39;AAA&#39;){
alert (json.msg);
}else{
window.location=json.page;
}
}
function send(){
var ul=&#39;a=tec&amp;name=&#39;+document.getElementById(&#39;name&#39;).value+&#39;&amp;password=&#39;+hex_md5(document.getElementById(&#39;pass&#39;).value)+&#39;&amp;yzm=&#39;+document.getElementById(&#39;yzm&#39;).value;
login.send(ul,&#39;login.php&#39;);
}
</script>
<link href=&#34;css.css&#34; rel=&#34;stylesheet&#34; type=&#34;text/css&#34; />
</head>
<body>
<div id=&#34;login&#34;>
<div align=&#34;center&#34;>管理登录 </div>
<form id=&#34;form&#34; name=&#34;form&#34; method=&#34;post&#34; onsubmit=&#34;return checkfm(form)&#34; action=&#34;javascript:send()&#34;>
<table width=&#34;100%&#34; border=&#34;0&#34; cellpadding=&#34;0&#34; cellspacing=&#34;0&#34;>
<tr>
<td width=&#34;27%&#34; height=&#34;30&#34;><div align=&#34;right&#34;>用户名</div></td>
<td colspan=&#34;2&#34;><input name=&#34;name&#34; type=&#34;text&#34; id=&#34;name&#34; size=&#34;20&#34; maxlength=&#34;20&#34; /></td>
</tr>
<tr>
<td height=&#34;30&#34;><div align=&#34;right&#34;>密  码</div></td>
<td colspan=&#34;2&#34;><label>
<input name=&#34;pass&#34; type=&#34;password&#34; id=&#34;pass&#34; value=&#34;&#34; size=&#34;20&#34; />
</label></td>
</tr>
<tr>
<td height=&#34;30&#34;><div align=&#34;right&#34;>验证码</div></td>
<td width=&#34;13%&#34;><input name=&#34;yzm&#34; type=&#34;text&#34; id=&#34;yzm&#34; size=&#34;6&#34; maxlength=&#34;4&#34; /></td>
<td width=&#34;60%&#34;><img src=&#34;yzm.php&#34; id=&#34;Image1&#34; onclick=&#34;Image1.src=&#39;./yzm.php?&#39;+Math.random()&#34; alt=&#34;点击刷新&#34; /></td>
</tr>
</table>
<div align=&#34;center&#34;>
<input type=&#34;submit&#34; name=&#34;Submit&#34; value=&#34;登录&#34; />
<input type=&#34;reset&#34; name=&#34;Submit2&#34; value=&#34;重置&#34; />
</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(&#39;text/xml&#39;);
}
}else if (window.ActiveXObject){
try{
http_request = new ActiveXObject(&#34;Msxml2.XMLHTTP&#34;);
}
catch (e){
try{
http_request = new ActiveXObject(&#34;Microsoft.XMLHTTP&#34;);
}catch (e)
{}
}
}
if (!http_request){
alert(&#39;Giving up <img src="http://fourier.eblhost.cn/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley">  Cannot cr&#101;ate an XMLHTTP instance&#39;);
return false;
}
return http_request;
}
_this.send=function(ul,page){
df = getXmlHttp();
df.onreadystatechange = onChange;
df.open(&#34;post&#34;,page,true);
df.setRequestHeader(&#34;Content-Type&#34;,&#34;application/x-www-form-urlencoded;&#34;);
df.send(ul);
}

_this.callback=function(json){
alert(json);
}

var onChange=function(){
if(df.readyState == 4 ){
if(df.status == 200) {
json = eval(&#39;(&#39; + df.responseText + &#39;)&#39;);
_this.callback(json);
}
}
}
}

login.php


include_once(&#39;db.php&#39;);
session_start();
//首先验证验证码的正确性
if(isset($_POST[&#39;yzm&#39;]) and strtolower($_POST[&#39;yzm&#39;])==strtolower(trim($_SESSION[&#39;yzm&#39;]))){
if(isset($_POST[&#39;a&#39;]) and $_POST[&#39;a&#39;]==&#39;tec&#39;){
$name=$_POST[&#39;name&#39;];
$password=$_POST[&#39;password&#39;];
try {
$dbh = new PDO($dsn);
$sql=&#34;sel&#101;ct * from user wh&#101;re name=&#39;&#34;.$name.&#34;&#39;&#34;;
$sth = $dbh->query($sql)->fetch(); $dsn= null;
if ($sth==&#34;&#34;){ $arr = array (&#39;msg&#39;=>&#39;该用户不存在!&#39;);
}else{ if($password==md5($sth[&#39;password&#39;])){
$_SESSION[&#39;t_name&#39;]=$name;
$_SESSION[&#39;t_id&#39;]=$sth[&#39;id&#39;];
$arr = array (&#39;msg&#39;=>&#39;AAA&#39;,&#39;page&#39;=>&#39;admin.php&#39;);
}else{ $arr = array (&#39;msg&#39;=>&#39;密码错误!&#39;);
}
}
} catch (PDOException $e) {
$arr = array (&#39;msg&#39;=>&#39;连接数据库失败!&#39;);
$dsn=null;
}
}
}else{
//验证码错误则返回错误信息
$arr = array (&#39;msg&#39;=>&#39;验证码错误!&#39;);
}

Published by 阿飞 on
Tags: none

暂无评论