Http Login example using HttpUtils2
Introduction:
Hi members, this is my first code sharing in this forum. This example is using HttpUtils2 library included in B4A v4.0. Hope this Code Snippet would help those who need a simple example on how to get started with log in to a website using user id and password.
The details for this example:
1. The website (a free hosting website by 000webhost.com which provide MySQL database and php)
2. The B4A app has a log in screen with 2 TextEdits and a button.
3. Key in the following credentials and click the "Login" button to test the app:
- User ID: demo
- Password: 12345
4. If User ID and password are correct, it will show up "Welcome, B4A User". Otherwise, it will show "Wrong user name or password"
* Note: Since I use a free hosting. The return message will show some overhead such as <!-- Hosting24 Analytics Code -->. This will not display in paid hosting.
The MySQL database is created from Cpanel and the tbl_user table is created using phpMyadmin which contains 3 fields (user_id, user_name, password)
B4A code
PHP Webservice (login.php)
Please comment and feedback. Thanks.
Introduction:
Hi members, this is my first code sharing in this forum. This example is using HttpUtils2 library included in B4A v4.0. Hope this Code Snippet would help those who need a simple example on how to get started with log in to a website using user id and password.
The details for this example:
1. The website (a free hosting website by 000webhost.com which provide MySQL database and php)
2. The B4A app has a log in screen with 2 TextEdits and a button.
3. Key in the following credentials and click the "Login" button to test the app:
- User ID: demo
- Password: 12345
4. If User ID and password are correct, it will show up "Welcome, B4A User". Otherwise, it will show "Wrong user name or password"
* Note: Since I use a free hosting. The return message will show some overhead such as <!-- Hosting24 Analytics Code -->. This will not display in paid hosting.
The MySQL database is created from Cpanel and the tbl_user table is created using phpMyadmin which contains 3 fields (user_id, user_name, password)
B4A code
B4X:
#Region Project Attributes
#ApplicationLabel: HTTP Login
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private btnLogin As Button
Private Label1 As Label
Private Label2 As Label
Private Label3 As Label
Private Panel1 As Panel
Private txtPassword As EditText
Private txtUserID As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("frmlogin")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnLogin_Click
Dim strUsername As String = txtUserID.Text.Trim
If strUsername = "" Then
Msgbox("Please enter User ID", "Error")
Return
End If
Dim strPassword As String = txtPassword.Text.Trim
If strPassword = "" Then
Msgbox("Please enter Password", "Error")
Return
End If
Dim job1 As HttpJob
job1.Initialize("Login", Me)
job1.Download2("http://kbase.herobo.com/login.php", _
Array As String("user_id",strUsername,"password",strPassword))
ProgressDialogShow("Connecting to server...")
End Sub
Sub JobDone (Job As HttpJob)
ProgressDialogHide
Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
If Job.Success = True Then
Dim strReturn As String
Log(Job.GetString)
strReturn = Job.GetString
Label3.Text = strReturn
Else
Log("Error: " & Job.ErrorMessage)
ToastMessageShow("Error: " & Job.ErrorMessage, True)
End If
Job.Release
End Sub
PHP Webservice (login.php)
PHP:
<?php
$host = "mysql16.000webhost.com";
$db = "a1438837_db";
$user = "a1438837_id";
$pw = "a1438837";
$con = mysql_connect($host,$user,$pw) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET NAMES 'utf8'");
$uid = $_GET["user_id"];
$pwd = $_GET["password"];
$res = mysql_query("SELECT user_name FROM tbl_user WHERE user_id = '$uid' AND password = '$pwd'");
$count = mysql_num_rows($res);
if ($count == 0) {
echo 'Wrong user name or password!';
}
else {
if ($row = mysql_fetch_array($res)) {
echo 'Welcome, '.$row['user_name'];
}
}
?>
Please comment and feedback. Thanks.
Attachments
Last edited: