dqhgjay5753 2015-06-20 03:15
浏览 35

在LogIn期间检索数据库数据

I'm currently using Slim and Ajax to develop a mobile application. In my database I have a column which stores session codes for logins. I need to be able to once logged in, compare the username entered to the database and retrieve the sessionCode based on that.

My current PHP is this:

$app->post('/logIn/', 'logIn');
function logIn()
{       
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());
    //$hashedPassword = password_hash($q->password, PASSWORD_BCRYPT);


    $sql = "SELECT * FROM users where username=:username AND password=:password";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);         
        $stmt->bindParam("username", $q->username);
                    $stmt->bindParam("password", $q->password);
        $stmt->execute();
        //$row=$stmt->fetch(PDO::FETCH_ASSOC);
        $row=$stmt->fetch(PDO::FETCH_OBJ);
        //$verify = password_verify($q->password, $row['password']);
        $db = null;
        echo json_encode($row);                 
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
} //PHP 5.4 LogIn

$app->get('/getSessionCode/:username','getSessionCode');
function getSessionCode($username)
{
    $request = \Slim\Slim::getInstance()->request();
    $q = json_decode($request->getBody());

    $sql = "SELECT * FROM users WHERE username=:username";
    try{
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("username", $username);
        $stmt->execute();
        $row=$stmt->fetchALL(PDO::FETCH_OBJ);
        $dbh=null;
        echo json_encode($row);
    }
    catch(PDOException $e){
        if(db != null) $db = null;
                echo $e->getMessage();
    }
}

Ajax Code:

    $("#checkLogIn").click(function()
{   
    username = $("#enterUser").val();

    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: rootURL + '/logIn/',
        dataType: "json",
        data: checkLogIn(),
        success: function(data)
        {
            if(data != false)
            {                   
                $.ajax({
                    type: 'GET',
                    url: rootURL + "/getSessionCode/" + username,
                    dataType: "json",
                    success: sessionData
                });
            }
            else
            {
                alert("Username and/or Password was incorrect");
            }
        }
    })
});
function checkLogIn(data)
{               
    return JSON.stringify({
        "username": $("#enterUser").val(),
        "password": $("#enterPass").val(),
    });                 
}   
function sessionData(data){
        session = data.sessionCode;
        alert(username);
        alert(session);

        $.mobile.changePage("#mainMenu");
}

When I run the application and log in. It runs though no problem but when it reaches alert(session) it returns undefined. I have both session and username set globally as variables so that isn't an issue and when it reaches alert(username), it returns the username I entered.

  • 写回答

1条回答 默认 最新

  • dorv56831 2015-06-22 13:50
    关注

    I was able to solve my issue by adding in a for loop into the sessionData function

    function sessionData(data){
        for(var i = 0; i < 1; i++)
        {
            sessionStorage.code = data[i].sessionCode;
        }
    
            $.mobile.changePage("#mainMenu");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?