Jump to content
Posted 15 January 2014 - 08:49 AM
Posted 15 January 2014 - 03:39 PM
Simply use a SSL (or HTTPS) library on the Java side. Configure Apache (or Nginx, or whatever your PHP host is) to run HTTPS with a certificate. If you use self-signed certificates, install the public certificate on the client machine and load that for the SSL library.
Then formulate a HTTPS POST with the data in whatever format you want -- JSON, XML or x-www-form-urlencoded are common formats. Send it to the server. Wait for the reply.
On the server, in PHP, you read the input like so:
$contents = file_get_contents("php:" . "//input"); // split the string because gamedev.net
"php input" is a string that I for some reason can't post as-is into the forum software (!)
The input comes from the POST or PUT request to the URL from the request body.
And parse it (if it's JSON) like so:
$variables = json_decode($contents)
Typically, you store the user name, and the bcrypt() of the password salted with the username, in a database table. You can then check for proper login by simply doing a select where username=(thename) and bcrypt_password=bcrypt(thepassword + thename). This avoids storing the password in an easily recoverable format in the database, so if the database somehow leaks to the greater internet, the plaintext passwords are not revealed.
Also, do not put user arguments into strings for database queries; use a database API that allows you to use symbolic query parameters so there is no risk of SQL injection.
Edited by hplus0603, 15 January 2014 - 06:38 PM.
Posted 15 January 2014 - 04:58 PM
It looks like I'll be using Apache and I'll look into finding a good Java library for SSL. After reading up a bit on certificates I guess it makes sense for me to use a self-signed certificate for now as it's not a website that will be displayed.
I'm familiar with JSON so that shouldn't be a problem.
Thanks for the security tips. I figured I'd have to encrypt the data, and shield the database from SQL injections, but now I know what API to look for.
How would I read the input in PHP? I think you missed filling in the field by accident.
EDIT: My guess would be something like
$contents = $_GET["data"];
Edited by keywi, 15 January 2014 - 05:02 PM.
Posted 15 January 2014 - 06:40 PM
No, the input is the post / put request body, and is parsed from php input (the URL which I cannot post straight in this forum !)
A HTTP request has a URL, which may have a query string, some headers, and a body.
PHP will attempt to decode the query string, and provide the values of that query string in $_GET.
PHP will also attempt to decode the request body if it is in the format x-www-form-urlencoded (which looks like a query string) and provide the values of that decode in $_POST. However, it will not decode JSON or XML formatted request bodies, so you have to do that yourself.
Posted 16 January 2014 - 06:03 AM
Posted 16 January 2014 - 10:18 AM
Would you recommend any particular software to run a locally hosted version on a Windows machine?
Posted 17 January 2014 - 03:43 AM