This kind of software if very useful if your application have to work on the internet and log in through a mysql database or if you are developing a game with an online multiplayer. We will soon write an article on how to code a registration form, so you can complete the procedure!

How does the algorithm works

  1. First to all our Android app get the input data from the user and pass it through the post method to our PHP script on the server.
  2. Our PHP script has the task to connect with mysql database and check if username and password exist in the dedicated table.
  3. Then the script will sends to our device, encoding in JSON (JavaScript Object Notation) format, the result of the query.

Let’s write the code!

Mysql table

We need to create a dedicated table, in which we can insert all the user that can have access to our log in. To do this we have used three fields: id, username, password.

CREATE TABLE `users` (
`id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`username` VARCHAR( 30 ) NOT NULL ,
`password` VARCHAR( 30 ) NOT NULL
) ENGINE = INNODB;

The id can be useful to identify the users and it is an auto_increment field. Username and password are varchar, max length 30. You can obviously modify them according to your needs. Now you can insert users in your table. For example we have insert 'Andrew' as username and 'mda' as password. This is not the most secure way to transfer password via web, we will soon write an article on how to do it in a very secure way...

PHP script

The code is real simple. It takes username and password via post and check if they exist. To do this we have two different file. The first one is to config the database. The second is to make our algorithm works. Remember to change the configuration datas. sql.cmprogrammers.com is the host, cmprogrammers is the username, android is the name of the database and yourpassword is the password of your db.

config.php

mysql_connect(‘sql.cmprogrammers.com’,'cmprogrammers','yourpassword') or die(mysql_error());
mysql_select_db('android') or die(mysql_error());

login.php

include ('config.php');
$username=$_POST["username"];
$password=$_POST["password"];

$sql = "SELECT * FROM `users` WHERE `username` = '$username' AND `password` = '$password'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);
if($row != 0){
 $output="ok";
}
else{
 $output="error";
}
print(json_encode($output));
mysql_close(); 

NB: The opening and close of php is omitted to show you the code!

Android code

At this point we have to implement our app. We have used Android Studio, but the procedure is similar in all the others IDE. Create a new project and then two class file, the first one is standard and allow us the connection, the second it the one that interacts with the user. You have to change the package, because we have used cmprogrammers.com.login.

CustomHttpClient.java

MainActivity.java

image

Have a look on the MainActivity file and edit it step by step according to your needs. Remember to change the link of the php file.

response = CustomHttpClient.executeHttpPost( "http://www.cmprogrammers.com/script/login.php", postParameters);

Now create the xml file, that allow you to see the interface on your device.

activity_main.xml

image

NB: We have decide not to add graphic and design and keep it real simple as it is to allow the user to modify the design according to the needs

Internet permission

Finally we have to add the internet permission on the manifest. Edit the manifest and add it in this way:

uses-permission android:name="android.permission.INTERNET"

image

Made it, if you debug your app you will see the login works.

image image