Maybe something like:
-- forward declare the text fields local username local password local email local function urlencode(str) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else print( event.response ) end end local function handleButtonEvent( event ) if ( "ended" == event.phase ) then local URL = "http://web.web.com/yourscript.php?username=" .. urlencode( username.text ) .. "&password=" .. urlencode(password.text) .. "&email=" .. urlencode( email.text ) network.request(URL, "GET", networkListener) end end local function loginLink( event ) if ( "ended" == event.phase ) then composer.gotoScene("login") end end function scene:create(event) local screenGroup = self.view display.setDefault("background", 0, 3, 5) local icon = display.newImage("icon\_opt.png", 160, 70) screenGroup:insert(icon) username = native.newTextField( 160, 200, 180, 30 ) -- take the local off since it's forward declared username.placeholder = "Username" screenGroup:insert(username) password = native.newTextField( 160, 260,180, 30 ) -- take the local off since it's forward declared password.isSecure = true password.placeholder = "Password" screenGroup:insert(password) email = native.newTextField( 160, 320, 180, 30 ) -- take the local off since it's forward declared email.placeholder = "E-mail" screenGroup:insert(email) -- rest of your code
Now there are still major concerns with this. First of all, you’re access http:// which is not secure. This script as I have it written will transmit the password in the clear and anyone snooping on your network will be easily able to grab the username and password. THIS IS INCREDIBLY INSECURE.
How can you secure it?
-
Don’t use obvious keys like “username”, “password” and "email
-
Base64 encode the data before you transmit it and your script needs to know to base64 decode it. If you base64 encode it, you won’t need to URL encode it which is a benefit.
-
Use https:// instead of http://. Your server must be set up to run SSL but at least the data is encrypted during its transmission to the server.
A more secure version of this, would be:
-- forward declare the text fields local mime = require("mime") local username local password local email local function networkListener( event ) if ( event.isError ) then print( "Network error: ", event.response ) else print( event.response ) end end local function handleButtonEvent( event ) if ( "ended" == event.phase ) then local URL = "https://web.web.com/yourscript.php?a=" .. mime.b64( username.text ) .. "&b=" .. mime.b64(password.text) .. "&c=" .. mime.b64( email.text ) network.request(URL, "GET", networkListener) end end local function loginLink( event ) if ( "ended" == event.phase ) then composer.gotoScene("login") end end function scene:create(event) local screenGroup = self.view display.setDefault("background", 0, 3, 5) local icon = display.newImage("icon\_opt.png", 160, 70) screenGroup:insert(icon) username = native.newTextField( 160, 200, 180, 30 ) -- take the local off since it's forward declared username.placeholder = "Username" screenGroup:insert(username) password = native.newTextField( 160, 260,180, 30 ) -- take the local off since it's forward declared password.isSecure = true password.placeholder = "Password" screenGroup:insert(password) email = native.newTextField( 160, 320, 180, 30 ) -- take the local off since it's forward declared email.placeholder = "E-mail" screenGroup:insert(email) -- rest of your code
But this assumes you have control over your script that’s going to handle the input and store the data into a database and you can use https://. Here I used keys: a, b and c and all the data has been base64 encoded. On the server side if you’re using PHP it would be something like:
\<?php $username = mysqli\_real\_escape\_string( base64\_decode( $\_GET["a"]) ); $password = mysqli\_real\_escape\_string( base64\_decode( $\_GET["b"]) ); $email = mysqli\_real\_escape\_string( base64\_decode( $\_GET["c"]) ); // rest of your script echo "Success" // or whatever you want to communicate back to the Corona App as event.response. ?\>
Now this could be even more secure by making sure you only store the password in a one way encryption such as an MD5 hash. The server should add some additional text to the password (called a salt) before it’s hashed. Then when the user logs in, you pass in the password from the user, salt it, hash it and then compare to what’s in the database. You should never store user passwords in clear text. And MD5 is considered minimal security. An SHA hash with larger encryption keys is more secure. Most hackers can bust an MD5 hash pretty quickly these days. That’s why adding a salt string is important.
If you don’t have control over the server script, then you will have to make your URL, key-value pairs, encoding match what the server expects.
Rob