urgent: corona app for our senior project

hello everyone

for our senior project, one of the most important functions in our application is login and sign up users

i searched in the internet, but i didn’t find anything :( 

so, anyone can help me with a simple code or something

Thank you so much 

This is an older tutorial that I wrote before i started working for Corona Labs, but it should get you started:

http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/

Rob

i applied the code in the tutorial but it doesn’t work with me i don’t know why :frowning:

MY code:

1- lua code:

local mime = require("mime") local mime = require("mime") local json = require("json") local username = "nosa" local password = "123" local URL = "http://localhost/json.php?un=".. mime.b64(username) .. "&ps=" .. mime.b64(password) -- DEBUG: Show constructed url -- print ( "Remote URL: " .. URL ) local function loginCallback(event) -- perform basic error handling if ( event.isError ) then print( "Network error!") else -- return a response code print ( "RESPONSE: " .. event.response ) local data = json.decode(event.response) -- display a result to the user if data.result == 200 then -- player logged in okay, display welcome message print("Welcome back") else -- bad password, or player not found. Prompt user to login again print("Please try again") end end return true end -- make JSON call to the remote server network.request( URL, "GET", loginCallback )  

 2- login form (login.php):

\<html\>\<body\> \<form method='get' action = 'json.php'\> Username \<input name='un'/\>\<br /\> Password \<input type='password' name='ps' /\>\<br /\> \<input type='submit' value='Login' /\> \</form\>\</body\>\</html\>

3- connection code:

\<?php $db = new PDO('mysql:host=localhost;dbname=users;charset=utf8', 'root', ''); $db-\>setAttribute(PDO::ATTR\_ERRMODE, PDO::ERRMODE\_EXCEPTION); ?\>

4- json.php:

\<?php extract($\_GET); require('connection.php'); if(isset($\_GET["un"])) { $username = base64\_decode($\_GET["un"]); $password = base64\_decode($\_GET["ps"]); $dbresult = mysql\_query('SELECT \* FROM doctors WHERE name ='. mysql\_real\_escape\_string($username)); if (!$dbresult) { //echo "query failed"; $result = array(); $result["result"] = 403; $result["message"] = mysql\_error(); echo json\_encode($result); exit; } mysql\_free\_result($dbresult); $doc = mysql\_fetch\_array($dbresult, MYSQL\_ASSOC); if (strcmp($doc["password"],md5($password)) == 0) { $result = array(); $result["result"] = 200; $result["message"] = "Success"; $result["username"] = $doc["username"]; echo json\_encode($result); } else { //echo "password mismatch"; $result = array(); $result["result"] = 403; $result["message"] = "Forbidden"; echo json\_encode($result); } } else { $result = array(); $result["result"] = 400; $result["message"] = "Bad Request"; echo json\_encode($result); } exit; ?\>

and i got the following error:

{“result”:403,“message”:“No database selected”}

So please can you help me :frowning:

Where are you doing these two lines:

// Connecting, selecting database
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect: ’ . mysql_error());
mysql_select_db(DB_DSN) or die(‘Could not select database’);

 actually when i did these two lines, as in the tutorial, i got an error, so instead of that i used external connection file

<?php
$db = new PDO(‘mysql:host=localhost;dbname=users;charset=utf8’, ‘root’, ‘’);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

The database isn’t being selected some how.  There should be plenty off MySQL help on line to help with that.

Rob

can i create login function using sqlite database ? i think its easier than mysql

Sure.

This is an older tutorial that I wrote before i started working for Corona Labs, but it should get you started:

http://omnigeek.robmiracle.com/2012/04/15/using-corona-sdk-with-rest-api-services/

Rob

i applied the code in the tutorial but it doesn’t work with me i don’t know why :frowning:

MY code:

1- lua code:

local mime = require("mime") local mime = require("mime") local json = require("json") local username = "nosa" local password = "123" local URL = "http://localhost/json.php?un=".. mime.b64(username) .. "&ps=" .. mime.b64(password) -- DEBUG: Show constructed url -- print ( "Remote URL: " .. URL ) local function loginCallback(event) -- perform basic error handling if ( event.isError ) then print( "Network error!") else -- return a response code print ( "RESPONSE: " .. event.response ) local data = json.decode(event.response) -- display a result to the user if data.result == 200 then -- player logged in okay, display welcome message print("Welcome back") else -- bad password, or player not found. Prompt user to login again print("Please try again") end end return true end -- make JSON call to the remote server network.request( URL, "GET", loginCallback ) &nbsp;

 2- login form (login.php):

\<html\>\<body\> \<form method='get' action = 'json.php'\> Username \<input name='un'/\>\<br /\> Password \<input type='password' name='ps' /\>\<br /\> \<input type='submit' value='Login' /\> \</form\>\</body\>\</html\>

3- connection code:

\<?php $db = new PDO('mysql:host=localhost;dbname=users;charset=utf8', 'root', ''); $db-\>setAttribute(PDO::ATTR\_ERRMODE, PDO::ERRMODE\_EXCEPTION); ?\>

4- json.php:

\<?php extract($\_GET); require('connection.php'); if(isset($\_GET["un"])) { $username = base64\_decode($\_GET["un"]); $password = base64\_decode($\_GET["ps"]); $dbresult = mysql\_query('SELECT \* FROM doctors WHERE name ='. mysql\_real\_escape\_string($username)); if (!$dbresult) { //echo "query failed"; $result = array(); $result["result"] = 403; $result["message"] = mysql\_error(); echo json\_encode($result); exit; } mysql\_free\_result($dbresult); $doc = mysql\_fetch\_array($dbresult, MYSQL\_ASSOC); if (strcmp($doc["password"],md5($password)) == 0) { $result = array(); $result["result"] = 200; $result["message"] = "Success"; $result["username"] = $doc["username"]; echo json\_encode($result); } else { //echo "password mismatch"; $result = array(); $result["result"] = 403; $result["message"] = "Forbidden"; echo json\_encode($result); } } else { $result = array(); $result["result"] = 400; $result["message"] = "Bad Request"; echo json\_encode($result); } exit; ?\>

and i got the following error:

{“result”:403,“message”:“No database selected”}

So please can you help me :frowning:

Where are you doing these two lines:

// Connecting, selecting database
$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Could not connect: ’ . mysql_error());
mysql_select_db(DB_DSN) or die(‘Could not select database’);

 actually when i did these two lines, as in the tutorial, i got an error, so instead of that i used external connection file

<?php
$db = new PDO(‘mysql:host=localhost;dbname=users;charset=utf8’, ‘root’, ‘’);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

The database isn’t being selected some how.  There should be plenty off MySQL help on line to help with that.

Rob

can i create login function using sqlite database ? i think its easier than mysql

Sure.