Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Post and Get methods — Gideros Forum

Post and Get methods

loves_oiloves_oi Member
edited August 2012 in General questions
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.

Comments

  • 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
  • loves_oiloves_oi Member
    edited August 2012
    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 ?
  • MellsMells Guru
    edited August 2012
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • @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.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
  • loves_oiloves_oi Member
    edited August 2012
    How could i interact php file and lua file?
    Like something this?
     
    local load = nil
     
    local function onComplete(event)
     	print("size:", #event.data)
    	load()
    end
     
    local function onError()
        print("error")
    end
     
    local function onProgress(event)
    end
     UrlLoader.POST
    load = function()
    	local loader = UrlLoader.new("checklogin.php,UrlLoader.POST")
    	loader:addEventListener(Event.COMPLETE, onComplete)
    	loader:addEventListener(Event.ERROR, onError)
    	loader:addEventListener(Event.PROGRESS, onProgress)
    end
     
    load()
    1.Where should i put checklogin.php ?
    2.Should i use UrlLoader.POST or UrlLoader.GET ?
    3.How could i interact php file and lua file?
    Thanks in advance
  • loves_oiloves_oi Member
    edited August 2012
    Moreover
    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
  • @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&quot" rel="nofollow">http://yourdomain.com/checklogin.php&quot</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


  • loves_oiloves_oi Member
    edited August 2012
    @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&quot" rel="nofollow">http://yourdomain.com/checklogin.php&quot</a>; ,UrlLoader.POST)
    @ar2rsawseen ,
    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 ?
    local loader = UrlLoader.new("???????" ,UrlLoader.POST)
    b.
    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?
  • 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 1
        echo 1;
    }
    else
    {
        //did not logged in, let's echo 0
        echo 0;
    }
    ?>
    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&quot" rel="nofollow">http://localhost/mylogin.php&quot</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")
  • loves_oiloves_oi Member
    edited August 2012
    OK.My codes are these:

    main.lua
    local theText = TextField.new(nil, "placeholder")
    theText:setPosition(10,265)
    theText:setTextColor(0x007B25)
    stage:addChild(theText)
     
     
    -- Function to download a file
    function download(theURL)
      local loader = UrlLoader.new(theURL)
     
      function onComplete(event)
        value = event.data
    	print (value)
    	theText:setText(value)
     
        loader:removeEventListener(Event.COMPLETE, onComplete)
        loader:removeEventListener(Event.ERROR, onError)
      end
     
      function onError(event)
        theText:setText("Could not download the file")
      end
     
      loader:addEventListener(Event.COMPLETE, onComplete)
      loader:addEventListener(Event.ERROR, onError)
    end
     
    function load()
      download('<a href="http://localhost/main_login.php?myusername=admin&mypassword=admin'" rel="nofollow">http://localhost/main_login.php?myusername=admin&mypassword=admin'</a>)
    end
     
    local timer = Timer.new(30000,0)
    timer:addEventListener(Event.TIMER, load)
    timer:start()

    main_login.php
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    	<tr>
    		<form name="form1" method="post" action="checklogin.php">
    			<td>
    				<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    					<tr>
    						<td colspan="3"><strong>Member Login </strong></td>
    					</tr>
    					<tr>
    						<td width="78">Username</td>
    						<td width="6">:</td>
    						<td width="294"><input name="myusername" type="text" id="myusername"></td>
    					</tr>
    					<tr>
    						<td>Password</td>
    						<td>:</td>
    						<td><input name="mypassword" type="text" id="mypassword"></td>
    					</tr>
    					<tr>
    						<td>&nbsp;</td>
    						<td>&nbsp;</td>
    						<td><input type="submit" name="Submit" value="Login"></td>
    					</tr>
    				</table>
    			</td>
    		</form>
    	</tr>
    </table>

    check_login.php
    <?php
    session_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 row
    if($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
    <?php
    echo "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:
    $myusername=$_GET['myusername']; 
    $mypassword=$_GET['mypassword'];
Sign In or Register to comment.