php
session_start();
include ('conn.php');
$action = $_GET['action'];
if ($action == 'login') { //登录
$email = $_POST['email'];
$pass = $_POST['password'];
if (empty ($email)) {
echo '邮箱不能为空';
exit;
}
if (empty ($pass)) {
echo '密码不能为空';
exit;
}
// $md5pass = md5($password);
$query = mysql_query("select * from user where email='$email'");
$us = is_array($row = mysql_fetch_array($query));
$ps = $us ? $pass == $row['password'] : FALSE;
if ($ps) {
$counts = $row['login_counts'] + 1;
$_SESSION['email'] = $row['email'];
$_SESSION['login_time'] = $row['login_time'];
$_SESSION['login_counts'] = $counts;
$ip = get_client_ip();
$logintime = mktime();
$rs = mysql_query("update user set login_time='$logintime',login_ip='$ip',login_counts='$counts'");
if ($rs) {
echo '1';exit;
$arr['success'] = 1;
$arr['msg'] = '登录成功!';
$arr['email'] = $_SESSION['email'];
$arr['login_time'] = date('Y-m-d H:i:s',$_SESSION['login_time']);
$arr['login_counts'] = $_SESSION['login_counts'];
} else {
$arr['success'] = 0;
$arr['msg'] = '登录失败';
}
} else {
$arr['success'] = 0;
$arr['msg'] = '用户名或密码错误!';
}
echo json_encode($arr);
}
elseif ($action == 'logout') { //退出
unset($_SESSION);
session_destroy();
echo '1';
}
```
js
```
// JavaScript Document
$(function(){
$("#email").focus();
$("input:text,textarea,input:password").focus(function() {
$(this).addClass("cur_select");
});
$("input:text,textarea,input:password").blur(function() {
$(this).removeClass("cur_select");
});
$("#btn-login").bind('click',function(){
var email = $("#email").val();
var password = $("#password").val();
if(email==""){
$(".control-group-email").removeClass('has-success').addClass("has-error");
$('
$("#email").focus();
return false;
}else{
$(".control-group-email").removeClass("has-error").addClass("has-success")
$('.help-block-text').remove();
$("#password").focus();
}
if(password==""){
$(".control-group-password").removeClass("has-success").addClass("has-error");
$('
$("#password").focus();
return false;
}else{
$(".control-group-password").removeClass("has-error").addClass("has-success")
$('.help-block-text').remove();
$("#btn-login").focus();
}
$.ajax({
type: "POST",
url: "login.php?action=login",
dataType: "json",
data: {"email":email,"password":password},
beforeSend: function(){
$('
);
},
success: function(json){
if(json.success==1){
$("#login-form").remove();
var div = "
";$("#login").append($('div'));
}else{
// $("#msg").remove();
$('
return false;
}
}
});
});
$("#logout").bind('click',function(){
$.post("login.php?action=logout",function(msg){
if(msg==1){
$("#result").remove();
var div = '
';$("#login").append(div);
}
});
});
});
```
index.php
```