ajax 登录 以及 修改密码 (thinkphp3.2)

登录 :

html


<!doctype html>
<html lang="zh-cn">
<head>
<title></title>
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Cache-Control" content="no-siteapp">
<link rel="stylesheet" href="CSS/style.login.css">
<script type="text/javascript" src="JS/jquery-1.11.1.min.js"></script>
<script src="JS/jquery-1.8.3.min.js"></script>
</head>
<body >
<div class="head"><h1></h1></div>
<div class="main" id="LOGIN">
<p style="color:#FFFFFF;font-size:20px;text-align:center"><p>
<div class="group-form after">
<form action="" method="post" name="login_main" id="login_main">
<div class="label">用户名:</div>
<div class="el"><i class="u"></i><input tabindex="1" id="admin_name" type="text" name="admin_name"/></div>
<div class="label">密 码:</div>
<div class="el"><i class="p"></i><input tabindex="2" id="admin_password" type="password" name="admin_password" /></div>
<div class="el"><button type='button' class="submit" id="btn" >登 录</button></div>
</form>
<div class="label"></div>
</div>
</div>
</div>
</body>
<script type='text/javascript'>
$(function(){
$('.submit').click(function(){
var admin_name=$("#admin_name").val();
var admin_password=$("#admin_password").val();
if(admin_password==""||admin_name==""){
alert('登录名与密码不能为空');
$("#admin_name").focus();
return false;
}else{
var url = "{:U('Admin/Login/check')}";
$.post(url, { admin_name:admin_name, admin_password:admin_password}, function(msg){
if(msg.info == 'ok') {
alert('登录成功,正在转向后台主页!');
window.location.href = msg.callback;
} else {
alert(msg.info);
}
}, 'json').error(function(){
alert("网络连接错误,请稍后再试");
});

}
})

});
</script>
</html>



php

<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
//后台登录页面
public function index()
{
$this->display(':login');
}

//检验管理员登录是否正确
public function check()
{
$admin_name = I('admin_name', '');
$admin_password = I('admin_password', '');
$admin_password = md5($admin_password);

$filter = array(
'name' => $admin_name,
'password' => $admin_password
);

$admin_info = M('user')->where($filter)->find();

if($admin_info){
session('admin_id',$admin_info['id']);
session('name',$admin_name);
$callback = U('Admin/Index/index');

$data = array(
'info' => 'ok',
'callback' => $callback
);
}else{
$data = array(
'info' => '登录失败,请检查登录名和密码是否正确'
);
}

$this->ajaxReturn($data);

}
public function logout()
{
session_unset();
session_destroy();
//删除用于自动登录的COOKIE
@setcookie('auto', '', time() - 3600, '/');
//跳转致登录页
redirect(U('Login/index'));

}

}



修改密码
html

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<script language="javascript" type="text/javascript" src="JS/DatePicker/WdatePicker.js"></script>
<link href="JS/DatePicker/skin/WdatePicker.css" rel="stylesheet" type="text/css">
<style type="text/css">
div{
width:100%;
}
</style>
<!--[if lt IE 9]>
<script type="text/javascript" src="JS/html5.js"></script>
<script type="text/javascript" src="JS/respond.min.js"></script>
<script type="text/javascript" src="JS/PIE_IE678.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="CSS/H-ui.css"/>
<link type="text/css" rel="stylesheet" href="CSS/H-ui.admin.css"/>
<link type="text/css" rel="stylesheet" href="FONT/font-awesome.min.css"/>
<!--[if IE 7]>
<link href="FONT/font-awesome-ie7.min.css" rel="stylesheet" type="text/css" />
<![endif]-->
<title>修改内容</title>
</head>
<body>
<div class="pd-20">
<div class="Huiform">
<form action="{:U('Admin/User/save')}" method="post" name="myform" id="editform" enctype="multipart/form-data">
<input type="hidden" value="{$admin_info['id']}" name='id'>
<table class="table table-bg">
<tbody>
<tr>
<th width="100" class="text-r"><span class="c-red">*</span> 用户名:</th>
<td><input type="text" style="width:200px" class="input-text" value="{$admin_info['name']}" placeholder="" id="user-name" name="username" datatype="*2-25" nullmsg="用户名不能为空" readonly="readonly"></td>
</tr>
<tr>
<th class="text-r">原始密码:</th>
<td> <input type="password" class="input-text" name="old_admin_password" placeholder="请输入原始密码" datatype="*3-16" nullmsg="请输入原始密码!" errormsg="密码范围在3~16位之间!"/>
<span class="Validform_checktip"></span></td>
</tr>
<tr>
<th class="text-r">新密码:</th>
<td> <input type="password" class="input-text" name="admin_password" placeholder="密码为3-16位字符串" datatype="*3-16" nullmsg="请设置密码!" errormsg="密码范围在3~16位之间!"/>
<span class="Validform_checktip"></span></td>
</tr>
<tr>
<th class="text-r">确认密码:</th>
<td> <input type="password" name="re_admin_repassword" class="input-text" placeholder="再次输入密码" datatype="*" ignore="ignore" recheck="admin_password" nullmsg="请再输入一次密码!" errormsg="您两次输入的账号密码不一致!" />
<span class="Validform_checktip"></span></td>
</tr>
<tr>
<th></th>
<td><button class="btn btn-success radius" type="submit"><i class="icon-ok"></i> 确定</button>&nbsp;&nbsp;<a href="{:U('Admin/User/update')}" class="btn btn-success radius"><i class="icon-ok"></i> 返回</a></td>
</tr>
</tbody>
</table>
</form>
</div>
</div>
<script type="text/javascript" src="JS/jquery.min.js"></script>
<script type="text/javascript" src="JS/Validform_v5.3.2_min.js"></script>
<script type="text/javascript" src="JS/H-ui.js"></script>
<script type="text/javascript" src="JS/H-ui.admin.js"></script>
<script type="text/javascript">
$(".Huiform").Validform();

$(function(){
<!-- 提交表单合法性验证 -->
$("#editform").Validform({
tiptype:function(msg,o,cssctl){
if(!o.obj.is("form")){
var objtip=o.obj.siblings(".Validform_checktip");
cssctl(objtip,o.type);
objtip.text(msg);
}
},
ajaxPost:true,
callback:function(data){
if(data.status=="ok"){
alert('密码修改成功,请退出重新登录!');
top.location.href = "{:U('Admin/Login/logout')}";
}else{
alert(data.info);
}
}
});

});
</script>

</body>
</html>

php


<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
//修改密码页面
public function update()
{
$admin_id = session('admin_id');
$admin_info = M('user')->find($admin_id);

$this->assign('admin_info', $admin_info);
$this->display();
}

//保存修改信息
public function save()
{
$admin_id = I('id');
$admin_name = I('username', '');
$old_admin_password = md5(I('old_admin_password', ''));
$admin_password =md5( I('admin_password', ''));
$re_admin_repassword = md5(I('re_admin_repassword', ''));

//是否一致
if($admin_password !== $re_admin_repassword){
$this->error('两次密码输入不一致');
}

//原始用户名和密码是否正确
$filter = array(
'name' => $admin_name,
'password' => $old_admin_password
);

$admin_info = M('user')->where($filter)->find();
if(!$admin_info){
$this->error('原始用户名或密码错误');
}else{
//更新管理员信息
$admin_info = array(
'password' => $re_admin_repassword
);
$result = M('user')->where(array('id' => $admin_id))->save($admin_info);
if($result){
$this->ajaxReturn(array('status' => 'ok', 'info' => '管理员信息修改成功'));
}else{
$this->ajaxReturn(array('status' => 'error', 'info' => '管理员信息修改失败'));
}
}

}

}





posted @ 2017-05-05 11:02  大咸菜·  阅读(2058)  评论(1编辑  收藏  举报