我正在创建一个需要用户登录的应用。当我单击登录按钮时,我只得到else语句。我检查了我的控制台日志,但没有显示,没有错误。我尝试了很多次,我在Corona SDK论坛上问过,但没有任何帮助。这是我的代码。
login.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
print( "Network error.")
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("userarea")
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 alert = native.showAlert( "Error Logging In", "There was an error logging in.", { "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.comxa.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
Login.PHP:
session_start();
// Check connection
if ($con->connect_error) {
die("Check connection.");
}
$username = ($_POST["username"]);
$pw = ($_POST["pw"]);
if(isset($_POST['Login'])) {
$username = mysqli_real_escape_string($con, $_POST["username"]);
$pw = mysqli_real_escape_string($con, $_POST["pw"]);
$sql = "SELECT * FROM users where username = '" . $username . "' and pw = '" . $pw . "'";
$result = mysqli_query($con, $sql);
$count = mysqli_num_rows($result);
if($count == 1){
$_SESSION['logged in'] = true;
}
else
{
echo"username or password is incorrect.";
}
}
答案 0 :(得分:0)
在建立连接之前,您需要设置标头。尝试
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 headers = {}
headers["Content-Type"] = "application/x-www-form-urlencoded"
headers["Accept-Language"] = "en-US"
parameters.headers = headers
local URL = "http://hash.comxa.com/login.php"
network.request(URL, "POST", networkListener, parameters)
end
end
end