php+ajax实现登录验证
2016-02-27 21:21
381 查看
login.js
$(document).ready(function(){ //提交表单 $('#submit_btn').click(function(){ show_loading(); //var myReg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; //邮件正则 if($('#account').val() == ''){ show_err_msg('账号还没填呢!'); $('#account').focus(); //}//else if(!myReg.test($('#email').val())){ //show_err_msg('您的邮箱格式错咯!'); //$('#email').focus(); }else if($('#password').val() == ''){ show_err_msg('密码还没填呢!'); $('#password').focus(); }else{ //show_msg('登录成功咯! 正在为您跳转...','shop.php'); postdata(); //button被点击时执行postdata函数 } }); }); function postdata() { $.ajax({ type: "post", url: "check.php", data:"&checkcode="+$("#j_captcha").val()+"&account="+$("#account").val()+"&pass="+$("#password").val(), error: function(){ alert('Error loading php document'); }, success: function(data){ if (data == "success") {show_msg('登录成功咯! 正在为您跳转...','shop.php'); } else if(data == "codefalse"){ show_err_msg('验证码输入错误!'); $('#j_captcha').focus(); }else {show_err_msg('账号或密码错误!');} } }); } <script type="text/javascript" src="js/login.js"></script><!---正则表达判断登录信息格式----> </head> <body> <div class="page-container"> <div class="main_box"> <div class="login_box"> <div class="login_logo"> <img src="images/logo.png" > </div> <div class="login_form"> <div class="form-group"> <label for="j_username" class="t">账 号:</label> <input id="account" name="account" type="text" class="form-control x319 in" autocomplete="off"> </div> <div class="form-group"> <label for="j_password" class="t">密 码:</label> <input id="password" name="pass" type="password" class="password form-control x319 in"> </div> <div class="form-group"> <label for="j_captcha" class="t">验证码:</label> <input id="j_captcha" name="checkcode" type="text" class="form-control x164 in" onblur="if(this.value==''){this.value='验证码:'}" onclick="if(this.value=='验证码:'){this.value='';}"> <img src="../inc/cc.php" height="40"> <a id="kanbuq" href="javascript:void(0);" onClick="$(this).prev().attr('src','../inc/cc.php?'+new Date())">看不清,换一张</a> </div> <div class="form-group"> <label class="t"></label> <label for="j_remember" class="m"> <input id="j_remember" type="checkbox" value="true"> 记住登陆账号!</label> <label id="div2"></label> </div> <div class="form-group space"> <label class="t"></label> <button type="submit" id="submit_btn" class="btn btn-primary btn-lg"> 登 录  </button> <input type="reset" value=" 重 置 " class="btn btn-default btn-lg"> </div> </div> </div> <div class="bottom">Copyright © 2015 - 2016 系统登陆</div> </div> </div>check.php
<?php include '../inc/db.php'; session_start(); $pc = strtolower(trim($_POST['checkcode'])); $sc = strtolower($_SESSION['code']); if($pc=='' || $pc!=$sc){ //header('location:login.html'); echo 'codefalse'; }else{ $a = $_POST['account']; $p = $_POST['pass']; $pass = m($p,$a); //$cuspass=query('customer','cuspassword',"cusaccount=$a"); $stmt = $db->prepare('select cuspassword,id from customer where cusaccount=? '); $stmt->execute([$a]); $rs = $stmt->fetchAll(3); //echo '<pre>'; //print_r( $rs); //echo $rs[0][0]; //exit; if($pass==$rs[0][0]){ $_SESSION['customer']=[$a,$rs[0][1]]; unset($_SESSION['code']); //header('location:shop.php'); echo 'success'; }else{ //header('location:login.html'); echo 'false'; } }
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- 解决Ajax悬停效果,无法遮蔽FLASH的问题
- 再谈Jquery Ajax方法传递到action(补充)
- Dom在ajax技术中的作用说明
- PHP+Apache在Windows 9x下的安装和配置
- 使用Ajax实时检测"用户名、邮箱等"是否已经存在
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- 探讨Ajax中同步与异步之间的区别