0

I'm currently working on a game in Java. I will be creating a launcher with the game itself and wrapping it with some .exe wrapper. What I need help with is getting the game to connect to some sort of server and check if the credentials for the user are true:

  1. Username is REAL.
  2. Password matches that listed with username.
  3. User has bought game (true/false) AKA is user premium?

It has to work with a website that uses PHP and HTML. When the user creates an account, it is stored both in the website, and in the game server itself. Also, if there's an update for the game, it automatically updates itself by getting the files from the server.

The concept is rather similiar to games like Minecraft and Planetside 2.

2 Answers2

1

You could build a php webservice and built a java web-service client. Using rest would be an option as well.

You could build a php socket server and connect to that socket.

You could build a php page and a java httpclient.

I'd make sure that it is encrypted somehow.

Udo Held
  • 12,314
  • 11
  • 67
  • 93
1

Depending on the complexity or level of interaction you require, this could be done with something as simple as making a GET request to a particular page on your web server, and supplying the necessary information in the query string (alternatively you could use POST).

// Construct the query string with the proper username and password
URL url = new URL("http://www.yoursite.com/verify.php?username=happy&password=pants");
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) {
    StringBuilder sb = new StringBuilder();
    for (String s; (s = in.readLine()) != null;)
        sb.append(s);
    switch (sb.toString().trim()) {
    case "welcome": 
        JOptionPane.showMessageDialog(null, "Welcome!");
        // Proceed to game
        break;
    case "shut up and pay me":
        JOptionPane.showMessageDialog(null, "You owe me money first!");
        break;
    case "no such user":
        JOptionPane.showMessageDialog(null, "Go away!");
        // Recursively delete all their files...
        break;
    }
}

Where verify.php has some PHP code that looks something like

// Set up database stuff...

// Query the database
$query = sprintf("SELECT * FROM friends users WHERE username='%s' AND password='%s'",
         mysqli_real_escape_string($_GET["username"]),
         mysqli_real_escape_string($_GET["password"]));

if (($result = mysqli_query($query)) && ($row = mysqli_fetch_assoc($result))) {
    if ($row["license"] == "premium") {
        echo "welcome";
    } else {
        echo "shut up and pay me";
    }
} else {
    echo "no such user";
}

But this is just a very basic example of what you could do, and shouldn't be followed verbatim. You probably shouldn't pass these values through the query string of the URL (perhaps prefer a POST rather than a GET here), and above all else you shouldn't store or pass the password in plain text either (i.e., hash it first using MD5, SHA1, or another hash of your choice). I only did this for the sake of simplicity.

For anything more complex, you might require one of the other methods you've been given, such as the use of sockets, web services, etc.

If you'd like more information on working with URLs in Java, that can be found here. Or if you'd like more information on PHP's Mysqli, you can get that here, or substitute your own package of choice like PEAR.

Community
  • 1
  • 1
sgbj
  • 2,264
  • 17
  • 14