ホームページ >バックエンド開発 >PHPチュートリアル >ajax session 登陆要刷新才能出数据

ajax session 登陆要刷新才能出数据

WBOY
WBOYオリジナル
2016-06-06 20:36:041350ブラウズ

php

<code><?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");
            $('<div class="help-block-text">').text("邮箱不能为空!").appendTo($('.control-group-email'));
            $("#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");
            $('<div class="help-block-text"></div>').text("密码不为空!").appendTo($('.control-group-password'));
            $("#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(){
                $('<div id="msg"></div>').addClass("loading").html("正在登录...").css("color","#999").appendTo('#btn-login').fadeOut(1000
                    );
            },
            success: function(json){
                if(json.success==1){
                    $("#login-form").remove();
                    var div = "<div id="result">
<p><strong>"+json.email+"</strong>,恭喜您登录成功!</p>
<p>您这是第<span>"+json.login_counts+
                    "</span>次登录本站。</p>
<p>上次登录本站的时间是:<span>"+json.login_time+
                    "</span></p>
<p><a href="#" id="logout">【退出】</a></p>
</div>";
                    $("#login").append($('div'));
                }else{
                    // $("#msg").remove();
                    $('<div id="errmsg"></div>').html(json.msg).css("color","#999").appendTo('.help-block').fadeOut(2000);
                    return false;
                }
            }
        });
    });
    $("#logout").bind('click',function(){
        $.post("login.php?action=logout",function(msg){
            if(msg==1){
                $("#result").remove();
                var div = '<div id="login-form">
<div class="control-group-email">
<label class="control-label">邮箱</label> <input type="email" class="form-control" name="email" id="email" placeholder="请输入可用的邮箱地址">
</div>
<div class="control-group-password">
<label class="control-label">密码</label> <input type="password" class="form-control" name="password" id="password">
</div>
<span id="helpBlock" class="help-block">没有帐号<a href="register.php">点击这里</a></span><button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal"><span class="fa fa-weibo" aria-hidden="true"></span></button><button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal"><span class="fa fa-weixin" aria-hidden="true"></span></button><button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal"><span class="fa fa-qq" aria-hidden="true"></span></button><div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button><button type="submit" class="btn btn-primary" id="btn-login">登录</button>
</div>
</div>';
                $("#login").append(div);
            }
        });
    });
});
```
index.php

```
<div id="login">
                                  
<?php if(isset($_SESSION['email'])){
?>



<div id="result">
<p><strong><?php echo $_SESSION['email'];?></strong>,恭喜您登录成功!</p>
<p>您这是第<span><?php echo $_SESSION['login_counts'];?></span>次登录本站。</p>
<p>上次登陆本站的时间是:<span><?php echo date('Y-m-d H:i:s',$_SESSION['login_time']);?>
</span></p>
<p><a href="#" id="logout">【退出】</a></p>
 </div>



                                  <?php }else{?>

                                  <div id="login-form">
                                    <div class="control-group-email">
                                      <label class="control-label">邮箱</label> 
                                        <input type="email" class="form-control" name="email" id="email" placeholder="请输入可用的邮箱地址">
                                    </div>
                                    <div class="control-group-password">
                                      <label class="control-label">密码</label> 
                                        <input type="password" class="form-control" name="password" id="password">
                                    </div>

                                    <span id="helpBlock" class="help-block">没有帐号<a href="register.php">点击这里</a></span>

                                    <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
                                        <span class="fa fa-weibo" aria-hidden="true"></span></button>
                                    <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
                                        <span class="fa fa-weixin" aria-hidden="true"></span></button>
                                    <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">
                                        <span class="fa fa-qq" aria-hidden="true"></span></button>
                             <div class="modal-footer">
                                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                                <button type="submit" class="btn btn-primary" id="btn-login">登录</button>
                            </div>
                          <?php } ?>
                        </div>


                            
                        


                                                                                                                        
                     <h2>回复内容:</h2>
                      
                                                            
<p>php</p>

<pre class="brush:php;toolbar:false"><code><?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");
            $('<div class="help-block-text"></code>
').text("邮箱不能为空!").appendTo($('.control-group-email')); $("#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"); $('
').text("密码不为空!").appendTo($('.control-group-password')); $("#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(){ $('
').addClass("loading").html("正在登录...").css("color","#999").appendTo('#btn-login').fadeOut(1000 ); }, success: function(json){ if(json.success==1){ $("#login-form").remove(); var div = "

"+json.email+",恭喜您登录成功!

您这是第"+json.login_counts+ "次登录本站。

上次登录本站的时间是:"+json.login_time+ "

【退出】

"; $("#login").append($('div')); }else{ // $("#msg").remove(); $('
').html(json.msg).css("color","#999").appendTo('.help-block').fadeOut(2000); 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 ```

,恭喜您登录成功!

您这是第次登录本站。

上次登陆本站的时间是:

【退出】

没有帐号点击这里

代码太破了,数据库连接,逻辑,html,js全面写在一起了。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:php 实例化对象疑惑?次の記事:ci3.0和laravel5.0该如何选择?

関連記事

続きを見る