php - 如何在corona sdk应用程序中显示用户名

标签 php android mysql mysqli lua

我正在使用 corona sdk 创建一个社交应用程序,用户可以单击两个选项卡。一个是新闻源,另一个是个人资料。当他们进入个人资料选项卡时,我希望他们看到自己的照片和用户名。问题是我的代码显示了我输入的内容,而不是任何用户的用户名。例如 local userName = tostring(userName) 这显示 nil 。此 local userName = tostring("user's username") 显示 用户的用户名 。如何在用户登录时显示他们的用户名?

配置文件.lua:

local composer = require( "composer" )
local scene = composer.newScene()

local widget = require("widget")
-- forward declare the text fields
local json = require("json")

local userName
local userNameText

local userName = tostring(userName)
display.newText( userName, display.contentCenterX, display.contentCenterY, 
native.systemFont, 20 )

local function networkListener( event )

if ( event.isError ) then

   local alert = native.showAlert( "Error Loading .", "Check your internet 
connection .", { "Try again" }  )

end
end

function scene:create(event)
local screenGroup = self.view 

local background = 
display.newImageRect("insta.jpg",display.contentWidth,display.contentHeight)
background.x = display.contentCenterX
background.y = display.contentCenterY
screenGroup:insert(background)

local userNameText = display.newText(userName, 200, 200, native.systemFont, 30 
)
userNameText:setFillColor( 1, 0, 0 )
screenGroup:insert(userNameText)

end

local tabButtons = {
{
    label = "#NewsFeed",
    width = 52, height = 10,
    id = "newsfeed",
    size = 16,
    onPress = function() composer.gotoScene("newsfeed"); end,
    selected = true
},
{
    label = "#Profile",
    size = 16,
    id = "profile",
    onPress = function() composer.gotoScene("profile"); end,
    selected = true
}
}   

-- Create the widget
local tabBar = widget.newTabBar(
{
    top = display.contentHeight -52,
    width = display.contentWidth,
    buttons = tabButtons,
}
)

function scene:show(event)

local phase = event.phase

if ( phase == "will" ) then
    print("Phase started")
elseif ( phase == "did" ) then
    print("phase on login")
end

composer.removeScene( "login" )

end

scene:addEventListener( "show" )

function scene:hide(event)

end

function scene:destroy(event)
end

scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)

return scene

配置文件.php:

include("auth_login.php");

echo $_SESSION['username']; 

auth_login.php:

if(!isset($_SESSION['username'])){
echo"Download the app"; 
die();
}

登录.lua:

local composer = require( "composer" )
local scene = composer.newScene()

local widget = require("widget")
-- forward declare the text fields
local json = require("json")

local username
local pw

local function emptyFields( event )
if ( username.text == "" or pw.text == "" ) then

      local alert = native.showAlert( "Empty fields", "Fill in all fields .", { "Try again" }  )

    return true 

    else
    return false

    end
    end

    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
        composer.gotoScene("newsfeed")

    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://hash.host22.com/login.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

local function registerLink( event )
if ( "ended" == event.phase ) then
     composer.gotoScene("register")
end
 end

  function scene:create(event)
  local screenGroup = self.view 

  local background = 
  display.newImageRect("bg4.jpg",display.contentWidth,display.contentHeight)
  background.x = display.contentCenterX
  background.y = display.contentCenterY
  screenGroup:insert(background)

  myImage = display.newImage( "hash.png" )
  -- position the image
  myImage:translate( 160, 70 )

  myText = display.newText( "#Hash", 160, 140, native.systemFontBold, 40 )
  myText:setFillColor( 1, 1, .5 )

username = native.newTextField( 160, 200, 180, 30 )  -- take the local off 
 since it's forward declared
 username.placeholder = "Username"
 screenGroup:insert(username)

 pw = native.newTextField( 160, 270,180, 30 ) -- take the local off since it's 
forward declared
pw.isSecure = true
 pw.placeholder = "Password"
 screenGroup:insert(pw)

local Button3 = widget.newButton(
{
    shape = "roundedRect",
    left = 70,
    top = 320,
    id = "Login",
    label = "Login",
    onEvent = userLogin,
    fillColor = { default={ 0, 1, 4, 0.7 }, over={ 1, 0.5, 0.8, 4 } },
    labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } }
}
)
screenGroup:insert(Button3)

local Button4 = widget.newButton(
{
    shape = "roundedRect",
    left = 70,
    top = 410,
    id = "register",
    label = "Register Here",
    onEvent = registerLink,
    fillColor = { default={ 2, 4, 0, 0.7 }, over={ 1, 3, 8, 4 } },
    labelColor = { default={ 2, 4, 1.5 }, over={ 2, 5, 1.5, 2.2 } }
}
)
screenGroup:insert(Button4)
end

function scene:show(event)

local phase = event.phase

if ( phase == "will" ) then
    print("Phase started")
elseif ( phase == "did" ) then
    print("phase on login")
end

composer.removeScene( "register" )

end

scene:addEventListener( "show" )

function scene:hide(event)
myText:removeSelf()
username:removeSelf()
pw:removeSelf()
myImage:removeSelf()

end

function scene:destroy(event)
end

scene:addEventListener("create", scene)
scene:addEventListener("show", scene)
scene:addEventListener("hide", scene)
scene:addEventListener("destroy", scene)

return scene

(当我在网站上执行所有 PHP 代码时,它会显示用户名。)

最佳答案

在您的login.lua中有部分代码:

if event.response == "success" then
    -- put the code here to go to where the user needs to be
    -- after a successful registration
    composer.gotoScene("newsfeed")

在这里您可以通过多种方式保存用户名:

1) 将 composer.setVariable 放在 composer.gotoScene 调用之前 - documentation

if event.response == "success" then
    composer.setVariable( "username", username.text )
    composer.gotoScene("newsfeed")

然后您可以在profile.lua中检索用户名:

local userName = composer.getVariable( "username" )

2)使用全局变量(最简单的解决方案)

USERNAME = username.text
composer.gotoScene("newsfeed")

并在个人资料中使用它

3)编写一些帮助程序来管理用户名(以及将来的用户首选项)

用户管理器.lua

local manager = {}

function manager.setUsername(username)
  manager.username = username
end

return manager

login.lua中:

if event.response == "success" then
    local userManager = require("userManager")
    userManager.setUsername(username.text)
    composer.gotoScene("newsfeed")

profile.lua中:

local userManager = require("userManager")
local username = userManager.username

关于php - 如何在corona sdk应用程序中显示用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45496145/

相关文章:

php - 如何使用 youtube api v3 获取 youtube 视频的完整描述

php - 如何通过选择文档中较早的链接来查看隐藏的 <span> ?

php - 如何在 PHP 中以整数形式返回枚举列值

android - 当我使用单选按钮时出现空指针异常

php - Laravel 不会链接样式表

android - Android 中带有标题的边框?

android - 手机右侧的空白

python - Django 查询优化 - 使用 Itertools

mysql - 加入和过滤两组相同的表

mysql - 检查 csv 文件中是否存在数据库记录