下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、PHP+Ajax验证码验证用户登录_ 用AJAX 验证用户登录的一个好处是不刷新跳转页面,外加用到验证码就更平安了,摸索的写了下。一共用到三个文件: yz.php: 生成验证码的PHP 文件,将验证码将在SESSION 里,供登录时对比调用 index.php: 用户登录的HTML 文件 loginCheck.php: 验证用户登录的文件 下面一一解析: yz.php 文件 ?php session_start(); /生成验证码图 Header(Content-type: image/PNG); /长与宽 $im = imagecreate(44,18); / 设置背景色: $back =
2、ImageColorAllocate($im, 245,245,245); / 填充背景色: imagefill($im,0,0,$back); srand(double)microtime()*1000000); $vcodes; /生成4位数字 for($i=0;$i4;$i+) $font = ImageColorAllocate($im, rand(100,255),rand(0,100),rand(100,255); $authnum=rand(1,9); $vcodes.=$authnum; imagestring($im, 5, 2+$i*10, 1, $authnum, $fo
3、nt); /加入干扰象素 for($i=0;$i100;$i+) $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255); imagesetpixel($im, rand()%70 , rand()%30 , $randcolor); ImagePNG($im); ImageDestroy($im); / 将四位的验证码保存在 SESSION 里,登录时调用对比 $_SESSIONVCODE=$vcodes; ? index.php: 留意,在这文件里不要取 $_SESSIONVCODE, 否则会取晚一步的
4、,刷新后才能显示上一个验证码 在 loginCheck.php 里验证就好了 !DOCTYPE html PUBLIC -/W3C/DTD HTML 4.01 Transitional/EN l4/loose.dtd html xmlns=l head meta http-equiv=Content-Type content=text/html;charset=gb2312 title管理后台| 请登录/title link rel=stylesheet type=text/css href=cssa.css style type=text/css !- #main font-family:宋
5、体; font-size:10pt; text-align:center; margin-top:510px; body background-attachment:fixed; background-position:center; background-image:url(./images/w2.jpg); background-repeat: no-repeat; #authCodebackground-Color:#F8F9FF; tabletext-align:center; /- /style script type=text/javascript src=./js/trim.js
6、/script script type=text/javascript !- function clearX() document.getElementById(authCode).value=; / 点击图片重新获得新的验证码: function getVCode() var vcode=document.getElementById(vcode); vcode.src =yz.php?nocache=+new Date().getTime(); /定义XMLHttpRequest对象 var xmlHttp; / 创建 XMLHttpRequest: function createXmlH
7、ttpRequest() var xmlHttp=null; try / Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); catch(e) / Internet Explorer try xmlHttp=new ActiveXObject(Msxml2.XMLHTTP); catch(e) xmlHttp=new ActiveXObject(Microsoft.XMLHTTP); return xmlHttp; / AJAX 检查登录: 有密码,要用POST 提交 function login() var authCode=t
8、rim(document.getElementById(authCode).value); var username=trim(document.getElementById(username).value); var password=trim(document.getElementById(password).value); if(username= | password= | authCode=) alert(请输入用户名和密码和验证码!); return false; else if(!xmlHttp) xmlHttp=createXmlHttpRequest(); var send_
9、string=username=+username+password=+password+authCode=+authCode+fresh=+Math.random(); xmlHttp.open(POST,loginCheck.php,true); xmlHttp.setRequestHeader(Content-Type,application/x-form-urlencoded); xmlHttp.send(send_string); xmlHttp.onreadystatechange=function() if(xmlHttp.readystate=4 xmlHttp.status=
10、200) var answer=xmlHttp.responseText; if(answer=ok) /跳转到管理中心页面 window.location.href=adminCenter.php; else alert(用户名密码或验证码不正确! 请重新输入!); document.getElementById(username).focus(); /- /script /head body onload=document.getElementById(username).focus(); div id=main table tr td用户名:input type=text id=user
11、name /td td密 码:input type=password id=password /td td验证码:input type=text id=authCode size=6 maxlength=4 value=验证码 onfocus=clearX()/td tdimg id=vcode src=yz.php alt=看不清?点击换一张 onclick=getVCode() /td tdinput id=loginButton type=submit value=登 录 onclick=login()/td /tr /table /div /body /html loginCheck.
12、php 验证用户登录的文件 ?php session_start(); include(./conn/connDB.php); / 取得POST过来的参数: $username=$_POSTusername; $password=md5($_POSTpassword); $authCode=$_POSTauthCode; $feedback=no; /对比是否=SESSION中的验证码,不能放在客户端做,否则取不正确的值 if($authCode=$_SESSIONVCODE) $SQL=select * from users where username=$username and password=$password; $res
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年农产品保鲜技术五年突破报告
- 2026年国家知识产权局专利局专利审查协作河南中心招聘60人备考题库及1套完整答案详解
- 2025至2030中国工业气体玻璃行业市场深度研究与战略咨询分析报告
- 2025至2030池塘过滤器行业发展研究与产业战略规划分析评估报告
- 库里签约协议书
- 定向开发协议书
- 2025至2030中国铷频率控制装置(RbXO)行业市场深度研究与战略咨询分析报告
- 2025年年度国家基本公共卫生服务项目培训试卷(附答案)
- 2025至2030山西省砂石开采行业发展现状调研及投资前景报告
- 2025公司项目部管理人员安全培训考试试题及参考答案
- 区间合同服务协议
- 2025福建三明市农业科学研究院招聘专业技术人员3人笔试考试备考题库及答案解析
- 《物业管理实务》教案
- 2025年南网能源公司社会招聘(62人)考试笔试参考题库附答案解析
- 2025年全科医师转岗培训理论考试题库(附答案)
- 《下肢深静脉血栓形成介入治疗护理实践指南》的解读2025
- 经营区域保护合同范本
- 医疗机构殡葬整治工作总结报告
- 2025年滁州辅警招聘考试真题及答案详解(历年真题)
- DB62T 3025-2018 钢结构工程施工工艺规程
- 基于多模型视角下我国A股上市公司财务危机预警的深度剖析与实证检验
评论
0/150
提交评论