I am trying to create code to let my users login . I tried fixing both sides (PHP and Lua) but I’m not getting anywhere . This is the same code I’m using in my website code so I don’t see why it’s not working .
loging.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("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 json = require("json") print( json.prettify( event ) ) 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://site.net16.net/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) composer.removeScene( "register" ) end 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
login.php:
\<?php error\_reporting(E\_ALL); ini\_set('display\_errors', 1); include("connect.php"); $\_SESSION['username'] = $\_POST['username']; //check if form is submitted if ( $\_SERVER['REQUEST\_METHOD'] != 'POST' || ! isset($\_POST['signin'])) { // looks like a hack, send to index.php header('Location: index.php'); die(); } if (empty($\_POST["username"])) { echo 'fill in username to sign in. \<a href= index.php\>Try again\</a\>\<br /\>'; die(); } if (empty($\_POST["pw"])) { echo 'fill in password to sign in. \<a href= index.php\>Try again\</a\>\<br /\>'; die(); } $sql = "SELECT pw FROM users WHERE username = ?"; $stmt = mysqli\_prepare($conn, $sql); if ( !$stmt ) { echo mysqli\_error($conn); die(); } $stmt-\>bind\_param('s', $\_POST['username']); if ( !$stmt-\>execute() ) { echo mysqli\_error($conn); die(); } // we found a row with that username, // now we need to check the password is correct // get the password from the row $stmt-\>bind\_result($hashed\_pwd); $stmt-\>fetch(); if ( password\_verify($\_POST['pw'], $hashed\_pwd) ) { // password verified $\_SESSION["username"] = $\_POST['username']; header('Location: profile.php'); } else { echo 'Incorrect username or Password. \<a href= index.php\>Try again\</a\>\<br /\>'; } ?\>