使用Corona SDK存储用户会话

我正在尝试使用Lua(Corona SDK)构建一个社交应用程序。我想做的是在用户登录/注册时保存用户会话,这样当他们关闭应用程序并再次打开它时不需要登录。

我有点困惑,因为我并不真的了解Lua,但我的代码不起作用。我使用PHP和MySQL作为后端和数据库,我已经在PHP中保存了用户的会话,但如何将会话ID显示到前端并验证它,以便我可以自动登录?

local function networkListener( event )

    if ( event.isError ) then

        local alert = native.showAlert("Error Logging In", "Check your internet connection .", {"Try again" })
    else if event.response == "success" then
        -- put the code here to go to where the user needs to be -- after a successful registration

        --username = userID

        composer.setVariable( "username", username.text )
        composer.gotoScene( "feed", { params = parametersToSend } )
        composer.removeScene( "login" )

    else -- put code here to notify the user of the problem, perhaps -- a native.alert() dialog that shows
        --them the value of event.response -- and take them back to the registration screen to let them try
        --again
        local json = require("json") print( json.prettify( event ) )
        local alert = native.showAlert( "Error Logging In", event.response , { "Try again" } )
    end
end
end

local function userLogin(event)
    if ( "ended" == event.phase ) then
        if emptyFields() == true then

        else

        local parameters = {}
        parameters.body = "Login=1&username=" .. username.text .. "&pw=" .. pw.text

        local URL = "http://192.168.1.37/hashmobile/process2.php"
        network.request(URL, "POST", networkListener, parameters)

        local headers = {}

        headers["Content-Type"] = "application/x-www-form-urlencoded"
        headers["Accept-Language"] = "en-US"

        parameters.headers = headers

        end
    end
end

process2.php:

$sql = "SELECT pw FROM users WHERE username = ?";
$stmt = mysqli_prepare($con, $sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->bind_result($hashed_pwd); //将变量绑定到准备好的语句,用于结果存储
$stmt->fetch(); //从准备好的语句中获取结果并将其存储到绑定的变量中

if (password_verify($pw, $hashed_pwd)) {

    //session_id($_POST['user_session_id']); //使用给定的会话ID启动会话
    // password verified
    $_SESSION["user_session_id"] = $username;
    echo "success";
    //echo $_SESSION["user_session_id"];
    //header('Location: profile.php');
    //die();

} else {

    //echo 'Incorrect username or Password.';
    die('Incorrect username or Password.');
}

这就是我登录到应用程序的方式。有帮助吗?

点赞