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");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料