I want to build a simple login page . What i need to do it ? Is there similar methods in Lua, like post get methods which exist in php? Thanks in advance.
Look at the UrlLoader documentation - you can use post and get methods with your request.
If you get it working it might make an interesting tutorial to share with the rest of the community.
WhiteTree Games - Home, home on the web, where the bits and bytes they do play! #MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
I'M so forgetful that i forgot where UrlLoader documentation is and now i cant find it. You mean reference_manual or seperate UrlLoader documentation ?
@loves_oi, the downloader function that you used in the previous question uses GET by default, you can change that to use POST, PUT, DELETE or GET as per your choice. The links to the reference shall be able to point you to the right direction.
You can also set the headers if you have to spoof the browser agent, etc.
to get my $myusername and $mypassword ? Or should i follow a different way? 5. Could i use SESSIONS while going from Lua to php? I'M closely pressed between Lua and Php :S
@loves_oi 1. checklogin.ph should be uploaded to your webserver with your domain. And then you can access it using
local loader = UrlLoader.new("<a href="http://yourdomain.com/checklogin.php"" rel="nofollow">http://yourdomain.com/checklogin.php"</a>; ,UrlLoader.POST)
2. It completely depends what you use on PHP side. If you use POST, then on PHP side you'll use $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; if you use GET, then on PHP side: $myusername=$_GET['myusername']; $mypassword=$_GET['mypassword'];
(Of course from PHP side you should sanitize string to prevent sql injections) (Another recommendation is to use SSL with HTTPS or somekind of encryption or at least username/password hashing on mobile side, before sending data)
3. Basically you can create REST API on server to interact with mobile app. Mobile app simply visits different URLs with different parameters to pass data and read response from server either json or xml (I mostly use json, and there are a lot of json parsers in lua). Here is an example of PHP REST API for scores I've created some time ago: http://appcodingeasy.com/Mobile-Backend/Score-REST-API-on-PHP5-and-MySQL
4. Yes exactly like that, only again don't forget to escape string to prevent injcetions if you use sql
5. I think you can't. You'll need to save session id and pass it as parameter. But maybe I'm mistaken and you can try when reusing same UrlLoader instance
what should i do to load a value to 'myusername' and 'mypassword' on the lua side?
c. And after doing it , when i reached at the success , what will happen? When i clicked ''run" on Gideros , the output should be in browser page or not?
On localhost it might not be that simple. Firstly you need to install a webserver as for example Apache on your machine. Create simple php script and call it mylogin.php like this:
<?php$myusername=$_POST['myusername'];$mypassword=$_POST['mypassword'];if($myusername=="username"&&$mypassword=="password"){//logged in, lets echo number 1echo1;}else{//did not logged in, let's echo 0echo0;}?>
Now you need to put this file into your server web directory (it's all based on the server you use and where you installed or configured it)
Then from the same PC Gideros player you could access it like this:
local url ="<a href="http://localhost/mylogin.php"" rel="nofollow">http://localhost/mylogin.php"</a>;
local loader = UrlLoader.new(url, UrlLoader.POST, {}, "myusername=username&mypassword=password")
local function onComplete(event)
if event.data == "1"then
--you have successfully logged in
else
--you did not logged in
end
end
local function onError()
print("error")
end
loader:addEventListener(Event.COMPLETE, onComplete)
loader:addEventListener(Event.ERROR, onError)
If you want to try it from device, then you'll need to input your PC's IP to url instead of localhost and probably configure your firewall settings to allow outside devices to access your webserver.
NOTE: this is just an example code explaining concept. I haven't tried if it's working.
i find some answers.Firstly i gave up using UrlLoader.POST , İ will use the default get method. and i use main_login.php?myusername=admin&mypassword=admin etc. for problem b and then i will output it to the simulator (problem c). But, is the adress right for localhost use:
local loader = UrlLoader.new("localhost/main_login.php?myusername=admin&mypassword=admin")
<?phpsession_start();$host="localhost";// Host name $username="";// Mysql username $password="";// Mysql password $db_name="test";// Database name $tbl_name="members";// Table name// Connect to server and select database.mysql_connect("$host","$username","$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");// username and password sent from form $myusername=$_POST['myusername'];$mypassword=$_POST['mypassword'];// To protect MySQL injection (more detail about MySQL injection)$myusername=stripslashes($myusername);$mypassword=stripslashes($mypassword);$myusername=mysql_real_escape_string($myusername);$mypassword=mysql_real_escape_string($mypassword);$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result);// If result matched $myusername and $mypassword, table row must be 1 rowif($count==1){// Register $myusername, $mypassword and redirect to file "login_success.php"$_SESSION['myusername']=$myusername;$_SESSION['mypassword']=$mypassword;//session_register("myusername");//session_register("mypassword"); header("location:login_success.php");}else{echo"Wrong Username or Password";}?>
login_success.php
<?php// Check if session is not registered, redirect back to main page. // Put this code in first line of web page. session_start();if(!isset($_SESSION[$myusername])){//echo "Login succeed";header("location:enter.php");}?>
<html>
<body>
Login Successful
</body>
</html>
enter.php
<?phpecho"Say hello to your dady"?>
I 'm trying to output "Say hello to your dady" on the simulator . but the output is the source code inside main_login.php.What should i do?
Well thats completely right, UrlLoader returns everything it gets from a webpage. Basically same thing that your browser outputs to you.
You see you don't really need a form. All you need is either pass UrlLoader POST data to check_login.php directly. Or pass it through UrlLoader GET data and then retrieve it inside check_login.php as:
Comments
If you get it working it might make an interesting tutorial to share with the rest of the community.
#MakeABetterGame! "Never give up, Never NEVER give up!" - Winston Churchill
This link will help : Reference Manual : UrlLoader
You can also set the headers if you have to spoof the browser agent, etc.
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Like something this?
2.Should i use UrlLoader.POST or UrlLoader.GET ?
3.How could i interact php file and lua file?
Thanks in advance
4.
if i use UrlLoader.POST in lua file , and sending the username and password to the php file, could i use
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
to get my $myusername and $mypassword ? Or should i follow a different way?
5. Could i use SESSIONS while going from Lua to php? I'M closely pressed between Lua and Php :S
1. checklogin.ph should be uploaded to your webserver with your domain. And then you can access it using
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
if you use GET, then on PHP side:
$myusername=$_GET['myusername'];
$mypassword=$_GET['mypassword'];
(Of course from PHP side you should sanitize string to prevent sql injections)
(Another recommendation is to use SSL with HTTPS or somekind of encryption or at least username/password hashing on mobile side, before sending data)
3. Basically you can create REST API on server to interact with mobile app. Mobile app simply visits different URLs with different parameters to pass data and read response from server either json or xml (I mostly use json, and there are a lot of json parsers in lua).
Here is an example of PHP REST API for scores I've created some time ago:
http://appcodingeasy.com/Mobile-Backend/Score-REST-API-on-PHP5-and-MySQL
4. Yes exactly like that, only again don't forget to escape string to prevent injcetions if you use sql
5. I think you can't. You'll need to save session id and pass it as parameter. But maybe I'm mistaken and you can try when reusing same UrlLoader instance
a.
I want to try it on my localhost now.Where should i put the file?Which file to where?WHAT SHOULD I WRITE for the adress ?
on the php side,
i am trying to do in this way
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
what should i do to load a value to 'myusername' and 'mypassword' on the lua side?
c.
And after doing it , when i reached at the success , what will happen? When i clicked ''run" on Gideros , the output should be in browser page or not?
Firstly you need to install a webserver as for example Apache on your machine. Create simple php script and call it mylogin.php like this:
Then from the same PC Gideros player you could access it like this:
NOTE: this is just an example code explaining concept. I haven't tried if it's working.
and i use main_login.php?myusername=admin&mypassword=admin etc. for problem b and then i will output it to the simulator (problem c).
But, is the adress right for localhost use:
local loader = UrlLoader.new("localhost/main_login.php?myusername=admin&mypassword=admin")
main.lua
main_login.php
check_login.php
login_success.php
enter.php
I 'm trying to output "Say hello to your dady" on the simulator . but the output is the source code inside main_login.php.What should i do?
You see you don't really need a form. All you need is either pass UrlLoader POST data to check_login.php directly. Or pass it through UrlLoader GET data and then retrieve it inside check_login.php as: