douguadao3883 2013-07-31 08:49
浏览 145

如何在Ajax登录系统中设置Cookie

I've created an AJAX login system in which I sent the data to login.php through ajax $.post request and it worked successfully. but I want to set cookie in login.php my it's not working why my code is as below Login.php for ajax request

    <?php
include ($_SERVER['DOCUMENT_ROOT'] . '/datastore/sqlconnect.php');
    $email = $_POST['email'];
    $password = CryptoLib::g_encode($_POST['password']);
    $query = mysqli_query($con, "SELECT f_name, password from `tablename` WHERE email='$email'");
    $data = mysqli_fetch_array($query);
    $pass = $data['password'];
    if($password == $pass)
    {
        $cookiedata = CryptoLib::g_encode($data['f_name']);
        setcookie('client',$cookiedata);
        echo 'success';

    }
    else
    {
        echo 'failed';
    }
?>

My Login page

<script type="text/javascript">
    $(document).ready( function() {
   $("#sidebarform").submit( function() {
    $("#loginbtn").attr("disabled", "disabled");
    $("#loginbtn").attr("value", "Logging in...");
    event.preventDefault();   
            $.post('ajaxhandlers/login.php',
                   $("#sidebarform").serialize(),function( result ) {
            if( result == 'success' ) {
            $("#msg").html("Logged in Successfully");
                            window.location="index.php";
            }
            else {
            $("#msg").html("Invalid Username or Password");
                            $("#loginbtn").attr("value", "Login");
                            $("#loginbtn").removeAttr("disabled");
            }
        });

        });
 });
</script>
  • 写回答

2条回答 默认 最新

  • dongmopu6734 2013-07-31 09:08
    关注

    Use this (or another ^^) jQuery Cookie plugin:
    https://github.com/carhartl/jquery-cookie

    Usage example:

    Create a cookie:

    $.cookie('the_cookie', 'the_value', { expires: 7, path: '/' });
    

    Read a cookie:

    $.cookie('the_cookie'); // => "the_value"
    $.cookie('not_existing'); // => undefined
    

    Good luck.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起