Android Code Snippet Register User example using OkHttpUtils2

Register User example using OkHttpUtils2

Demo1.png Demo2.png Demo3.png Demo4.png Demo5.png Demo6.png

Introduction
This code snippet is based on the thread posted by KMatle in Tutorials & Examples. You can modify the layout and add in to your apps so that registered members can use your app by logging in using their user id and password.

How it works
This app starts by checking for connection by sending a request to the server and wait for a reply. The PHP script in the server will process the request and send back a JSON string. The screen will show "Connection success" if it is getting a response from the server.

Tap on the 'Register' button and the app will open up a new activity where information such as user id, password and email are required for the registration process. The data submitted will be stored in a MySQL database. An email will be sent to the user. If the user id or email is already registered before, a message will show "The user id '[YourUserID]' or email ([YourEmail]) is already in use". Open the email and click on the link to finish the registration process.

After successful register, you can press the 'Login' button to open up the Login activity. If you try to log in without activating your account at the first place, the app will show a message "Account is not activated!". Key in your user id and password and tap on the 'Login' button. If user id and password are incorrect, the app will show a message "Wrong User ID or Password!". If log in success, the app will show a message "Welcome, [YourFullName]" in a new activity showing a list of registered members in a listview.

This code snippet demonstrates of the usage of :
1. HttpUtils2 to connect to MySQL database located in a free hosting provider (using PHP web services and JSONParser)
2. Multiple Activity and Layout
3. SQL commands to Insert and Update records in MySQL database tables
4. Regular expression to check the email format
5. PHP Mail() function to send mail to the new member for activation and notify the administrator.
6. ListView for populating records stored in MySQL database table

Note that you can add more functions such as password hashing, retrieve forgotten password and using Captcha verification.

Version 1.1 (Update 07 July 2015)
- Rename a field in database table tbl_member from "status" to "reg_status"
- Added "(Online)" beside user name in listview
- Added a Log out button and signout.php
- Fixed signin.php showing 'null' user name

Version 1.2 (Update 10 Mar 2016)
- Retrieve Password
- Reset Password

Reupload PHP files (Update 19 Apr 2016)
Due to many confusion of register.php in a line of code somewhere in signup.php script, I have reuploaded PHP.zip in attachment. Sorry for the inconvenience.

Version 1.3 (Update 08 Feb 2019)
- Set targetSdkVersion=26
- Replaced HttpUtils2 with OkHttpUtils2
- Changed conflicting Job names with module names
- Added Change Password activity
- Removed Forgot Password activity
- Replaced MySQL functions in PHP script with MySQLi functions
- Added db.php in PHP script to store global variables

B4A Codes
B4X:
#Region  Project Attributes
    #ApplicationLabel: Demo
    #VersionCode: 4
    #VersionName: 1.3
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: portrait
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    Dim strURL As String = "http://demo.computerise.my/b4a/register-user-php/"    ' Remote / Production
    'Dim strURL As String = "http://192.168.43.191:8000/register-user-php/"        ' Local / Development
End Sub

Sub Globals
    Private Panel1 As Panel
    Private lblTitle As Label
    Private lblVersion As Label
    Private btnLogin As Button
    Private btnRegister As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmStart")
    lblVersion.Text = "v 1.3"
    TestConnection
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Activity_KeyPress(KeyCode As Int) As Boolean
    Dim Answ As Int
    Dim Txt As String

    If KeyCode = KeyCodes.KEYCODE_BACK Then    ' Checks if the KeyCode is BackKey
        Txt = "Do you really want to quit the program ?"
        Answ = Msgbox2(Txt, "A T T E N T I O N", "Yes", "", "No", Null) ' MessageBox
        If Answ = DialogResponse.POSITIVE Then    ' If return value is Yes then
            Return False    ' the Event will not be consumed      
        Else                ' we leave the program
            Return True        ' the Event will be consumed to avoid
        End If                ' leaving the program
    Else
        Return True            ' the Event will be consumed to avoid
    End If
End Sub

Sub lblTitle_Click
    Dim p As PhoneIntents
    Dim Url As String = "http://computerise.my"
    StartActivity(p.OpenBrowser(Url))
End Sub

Sub btnLogin_Click
    StartActivity("Login")
End Sub

Sub btnRegister_Click
    StartActivity("Register")
End Sub

Sub TestConnection
    Dim Connect As HttpJob
    Connect.Initialize("Connect", Me)
    Connect.Download(strURL & "connect.php")
    ProgressDialogShow("Connecting to server...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim ret As String
        ret = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(ret)  
        If Job.JobName = "Connect" Then
            Dim act As String = parser.NextValue
            If act = "Connected" Then
                ToastMessageShow("Connection success", True)
            End If  
        End If
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Dim txtUserID As EditText
    Dim txtPassword As EditText
    Dim txtFullName As EditText
    Dim txtLocation As EditText
    Dim txtEmail As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmRegister")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnRegister_Click
    Dim strUserID As String = txtUserID.Text.Trim
    If strUserID = "" 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 strFullName As String = txtFullName.Text.Trim
    If strFullName = "" Then
        Msgbox("Please enter Full Name", "Error")
        Return
    End If
    Dim strLocation As String = txtLocation.Text.Trim
    If strLocation = "" Then
        Msgbox("Please enter Location", "Error")
        Return
    End If
    Dim strEmail As String = txtEmail.Text.Trim
    If strEmail = "" Then
        Msgbox("Please enter Email", "Error")
        Return
    End If
    If Validate_Email(strEmail) = False Then
        Msgbox("Email format is incorrect", "Error")
        Return
    End If

    Dim Job1 As HttpJob
    Job1.Initialize("Register", Me)
    Job1.Download2(Main.strURL & "signup.php", _
      Array As String("Action", "Register", _
      "UserID", txtUserID.Text, _
      "Password", txtPassword.Text, _
      "FullName", txtFullName.Text, _
      "Location", txtLocation.text, _
      "Email", txtEmail.Text))
    ProgressDialogShow("Connecting to server...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
        Dim parser As JSONParser
        Dim res As String
        Dim action As String
        res = Job.GetString  
        parser.Initialize(res)          
        Select Job.JobName
            Case "Register"
                action = parser.NextValue
                If action = "Mail" Then
                    Msgbox("An email was sent to " & txtEmail.Text & ". Please click on the link to finish registration", "Registration")
                    Activity.Finish
                Else If action = "MailInUse" Then
                    Msgbox("The user id '" & txtUserID.Text & "' or email (" & txtEmail.Text & ") is already in used", "Registration")
                Else
                    Msgbox("Server does not return expected result.", "Registration")
                End If
        End Select
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

' // Source: https://www.b4x.com/android/forum/threads/validate-a-correctly-formatted-email-address.39803/
Sub Validate_Email(EmailAddress As String) As Boolean
    Dim MatchEmail As Matcher = Regex.Matcher("^(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])$", EmailAddress)

    If MatchEmail.Find = True Then
        'Log(MatchEmail.Match)
        Return True
    Else
        'Log("Oops, please double check your email address...")
        Return False
    End If
End Sub

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals
    Dim strUserID As String
    Dim strUserName As String
End Sub

Sub Globals
    Dim txtUserID As EditText
    Dim txtPassword As EditText
    Dim lblMessage As Label
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 strUserID As String = txtUserID.Text.Trim
    lblMessage.Text = ""
    strUserID = txtUserID.Text.Trim
    If strUserID = "" 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 Job2 As HttpJob
    Job2.Initialize("Login", Me)
    Job2.Download2(Main.strURL & "signin.php", _
    Array As String("user_id", strUserID, "password", strPassword))
    ProgressDialogShow("Connecting to server...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim ret As String
        Dim act As String  
        ret = Job.GetString   
        Dim parser As JSONParser
        parser.Initialize(ret)  
        act = parser.NextValue
        If act = "Not Found" Then
            ToastMessageShow("Login failed", True)
            lblMessage.Text = "Wrong User ID or Password!"
            lblMessage.TextColor = Colors.Red
        Else If act = "Not Activated" Then
            ToastMessageShow("Login failed", True)
            lblMessage.Text = "Account is not activated!"
            lblMessage.TextColor = Colors.Blue      
        Else If act = "Error" Then
            ToastMessageShow("Login failed", True)
            lblMessage.Text = "An error has occured!"
            lblMessage.TextColor = Colors.Red
        Else
            strUserName = act
            StartActivity("Member")
            Activity.Finish
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

'Sub btnForgotMyPassword_Click
'    StartActivity("Forgot")
'End Sub

Sub btnResetMyPassword_Click
    StartActivity("Reset")
End Sub

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Type TwoLines (First As String, Second As String)
    Private ListView1 As ListView
    Private btnLogout As Button
    Private lblMessage As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmMember")
    lblMessage.Text = "Welcome, " & Login.strUserName
    LoadMemberList
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub LoadMemberList
    Dim Job3 As HttpJob
    Job3.Initialize("Member", Me)
    Job3.Download(Main.strURL & "member.php")
    ProgressDialogShow("Downloading list of registered members")
End Sub

Sub LogMeOut
    Dim Job4 As HttpJob
    Job4.Initialize("LogOut", Me)
    Job4.Download2(Main.strURL & "signout.php", _
    Array As String("user_id", Login.strUserID))
    ProgressDialogShow("Logging out...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim strReturn As String = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(strReturn)
        If Job.JobName = "Member" Then
            Dim Members As List
            Dim strOnline As String
            Members = parser.NextArray 'returns a list with maps
            For i = 0 To Members.Size - 1
                Dim m As Map
                m = Members.Get(i)
                Dim TL As TwoLines
                If m.Get("online") = "Y" Then
                    strOnline = " (Online)"
                Else
                    strOnline = ""
                End If
                TL.First = m.Get("user_id") & strOnline
                TL.Second = m.Get("location")
                ListView1.AddTwoLines2(TL.First, TL.Second, TL)
            Next
        Else If Job.JobName = "LogOut" Then
            Dim act As String = parser.NextValue
            If act = "LoggedOut" Then
                ToastMessageShow("Logout successful", True)          
                StartActivity(Main)
                Activity.Finish
            End If          
        Else
            ToastMessageShow("Error: Invalid Value", True)
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Sub btnChangePassword_Click
    StartActivity("Change")
End Sub

Sub btnLogout_Click
    LogMeOut
End Sub

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private txtEmail As EditText
    Dim strEmail As String
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmReset")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnSubmit_Click
    strEmail = txtEmail.Text.Trim
    If strEmail = "" Then
        Msgbox("Please enter Email", "Error")
        Return
    End If
    If Validate_Email(strEmail) = False Then
        Msgbox("Email format is incorrect", "Error")
        Return
    End If
    Dim Job5 As HttpJob
    Job5.Initialize("ResetPassword", Me)
    Job5.Download2(Main.strURL & "reset-password.php", _
      Array As String("Action", "RequestPasswordReset", _
      "Mail", strEmail))
    ProgressDialogShow("Connecting to server...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success Then
    Dim res As String, action As String
        res = Job.GetString  
        Dim parser As JSONParser
        parser.Initialize(res)
        Select Job.JobName
            Case "ResetPassword"
                action = parser.NextValue
                If action = "ValidEmail" Then
                    Msgbox("An email was sent to " & strEmail & " to reset your password.", "Reset Password")              
                Else If action = "InvalidEmail" Then
                    Msgbox("The email is not registered in our database.", "Reset Password")
                End If
        End Select
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

' // Source: https://www.b4x.com/android/forum/threads/validate-a-correctly-formatted-email-address.39803/
Sub Validate_Email(EmailAddress As String) As Boolean
    Dim MatchEmail As Matcher = Regex.Matcher("^(?i)[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])$", EmailAddress)

    If MatchEmail.Find = True Then
        'Log(MatchEmail.Match)
        Return True
    Else
        'Log("Oops, please double check your email address...")
        Return False
    End If
End Sub

B4X:
#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: False
#End Region

Sub Process_Globals

End Sub

Sub Globals
    Private txtEmail As EditText
    Private txtPassword1 As EditText
    Private txtPassword2 As EditText
    Private txtPassword3 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("frmChange")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnSubmit_Click
    Dim strEmail As String = txtEmail.Text.Trim
    If strEmail = "" Then
        Msgbox("Please enter Your Email", "Error")
        Return
    End If
    Dim strPassword1 As String = txtPassword1.Text.Trim
    If strPassword1 = "" Then
        Msgbox("Please enter Current Password", "Error")
        Return
    End If
    Dim strPassword2 As String = txtPassword2.Text.Trim
    If strPassword2 = "" Then
        Msgbox("Please enter New Password", "Error")
        Return
    End If
    Dim strPassword3 As String = txtPassword3.Text.Trim
    If strPassword3 = "" Then
        Msgbox("Please reenter New Password", "Error")
        Return
    End If
    If strPassword2 <> strPassword3 Then
        Msgbox("New Password not match", "Error")
        Return
    End If

    Dim Job6 As HttpJob
    Job6.Initialize("Change", Me)
    Job6.Download2(Main.strURL & "change-password.php", _
    Array As String("Email", strEmail, _
    "Password1", strPassword1, _
    "Password2", strPassword2))
    ProgressDialogShow("Connecting to server...")
End Sub

Sub JobDone (Job As HttpJob)
    ProgressDialogHide
    If Job.Success = True Then
        Dim ret As String
        Dim act As String
        ret = Job.GetString
        Dim parser As JSONParser
        parser.Initialize(ret)
        act = parser.NextValue
        If act = "Success" Then
            Msgbox("Password updated successfully!", "Change Password")
        Else If act = "Not Found" Then  
            Msgbox("Email or Password not correct!", "Change Password")
        Else If act = "Error" Or act = "Failed" Then
            Msgbox("An error occured!", "Change Password")
        Else ' Failed
            Msgbox("Uncaught error!", "Change Password")
        End If
    Else
        'Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

PHP Scripts

PHP:
<?php
    print json_encode("Connected");
?>

PHP:
<?php
    $host = "localhost";
    $user = "myuser";
    $pass = "mypassword";
    $db = "demo_b4a";
    $mysqli = new mysqli($host, $user, $pass, $db) or die($mysqli->error);

    // $server = "http://demo.computerise.my/b4a/";
    $server = "http://192.168.43.191:8000/register-user-php/";
    $admin = "mailer@computerise.my";
    $sender = "no-reply@computerise.my";
?>

PHP:
<?php
    require 'db.php';
    try
    {
        if(!isset($_GET['Action']) || empty($_GET['Action']))
        {
            print json_encode("Parameter Error");
            exit;
        }  
        $action = $mysqli->escape_string($_GET["Action"]);

        switch ($action)
        {
            case "Register":
                if (!isset($_GET['UserID']) || empty($_GET['UserID']) ||
                    !isset($_GET['Email']) || empty($_GET['Email']) ||
                    !isset($_GET['Password']) || empty($_GET['Password']) ||
                    !isset($_GET['FullName']) || empty($_GET['FullName']) ||
                    !isset($_GET['Location']) || empty($_GET['Location']))
                {
                    print json_encode("Parameter Error");
                    exit;
                }
                $user = $mysqli->escape_string($_GET["UserID"]);
                $email = $mysqli->escape_string($_GET["Email"]);
                $password = $mysqli->escape_string($_GET["Password"]);
                $fullname = $mysqli->escape_string($_GET["FullName"]);
                $location = $mysqli->escape_string($_GET["Location"]);
                $sql = "SELECT * FROM tbl_member";
                $sql .= " WHERE email = '".$email."'";
                $sql .= " OR user_id = '".$user."'";
                $result = $mysqli->query($sql);
                $count = $result->num_rows;
                if ($count == 0)
                {
                    $randomnumber = mt_rand(111111, 999999);
                    $sql = "INSERT INTO tbl_member";
                    $sql .= " (user_id, user_name, pass_word,";
                    $sql .= " email, location,";
                    $sql .= " reg_status, reg_no, online)";
                    $sql .= " VALUES (";
                    $sql .= " '$user', '$fullname', '$password',";
                    $sql .= " '$email', '$location',";
                    $sql .= " 'M', $randomnumber, 'N')";
                    $mysqli->query($sql);
                    $to      = $email;
                    $subject = "B4A Register User Demo";
                    $message = "Hi ".$user.","."\r\n";
                    $message .= "Please click on this link to finish";
                    $message .= " the registration process:";
                    $message .= " ".$server."signup.php?Action=Mail";
                    $message .= "&Mail=".$email;
                    $message .= "&RegNo=".$randomnumber;
                    $message = wordwrap($message, 70, "\r\n");
                    $headers = "From: ".$sender."\r\n";
                    $headers .= "Reply-To: ".$sender."\r\n";
                    $headers .= "X-Mailer: PHP/".phpversion();

                    mail($to, $subject, $message, $headers);
                    // Notify me of new sign up
                    $to      = $admin;
                    $subject = "New member";
                    $message = "New member (".$user.") has signed up using our demo app.";
                    mail($to, $subject, $message, $headers);
                    print json_encode("Mail");
                }
                else
                    print json_encode("MailInUse");
                break;
            case "Mail":
                if (!isset($_GET['RegNo']) || empty($_GET['RegNo']) ||
                    !isset($_GET['Mail']) || empty($_GET['Mail']))
                {
                    print json_encode("Parameter Error");
                    exit;
                }      
                $regno = $mysqli->escape_string($_GET["RegNo"]);
                $mail = $mysqli->escape_string($_GET["Mail"]);
                $sql = "SELECT *";
                $sql .= " FROM tbl_member";
                $sql .= " WHERE email = '$mail'";
                $sql .= " AND reg_no = $regno";
                $sql .= " AND reg_status = 'M'";
                $result = $mysqli->query($sql);
                $count = $result->num_rows;
                if ($count == 0)
                {
                    print json_encode("This registration is not valid / email address is already registered");
                }
                else
                {
                    $sql = "UPDATE tbl_member";
                    $sql .= " SET reg_status = 'R'";
                    $sql .= " WHERE email = '$mail'";
                    $sql .= " AND reg_no = $regno";
                    $mysqli->query($sql);
                    echo("$mail is registered now :-)");
                    print json_encode("Success");
                }
                break;
            default:
                echo("Unauthorized action! Please use the app to register.");
        }
    }
    catch (Exception $e)
    {
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    }  
?>

PHP:
<?php
    require 'db.php';
    try
    {
        if (!isset($_GET['user_id']) || empty($_GET['user_id']) ||
            !isset($_GET['password']) || empty($_GET['password']))
        {
            print json_encode("Parameter Error");
            exit;
        }   
        $uid = $mysqli->escape_string($_GET["user_id"]);
        $pwd = $mysqli->escape_string($_GET["password"]);

        $sql = "SELECT user_name, reg_status";
        $sql .= " FROM tbl_member";
        $sql .= " WHERE user_id = '".$uid."'";
        $sql .= " AND pass_word = '".$pwd."'";
        $result = $mysqli->query($sql);
        if ($mysqli->errno)
        {
            print json_encode("Error");
            echo "<br />".$mysqli->error;
            exit;
        }
        else
        {
            if ($result->num_rows == 0)
            {
                print json_encode("Not Found");
                exit;
            }
            $row = $result->fetch_row();
            if ($row[1] == "M")
            {
                print json_encode("Not Activated");
            }
            else
            {
                print json_encode($row[0]);
                $sql = "UPDATE tbl_member";
                $sql .= " SET Online = 'N'";
                $sql .= " WHERE now()-time_stamp > 60";       
                $mysqli->query($sql);
                $sql = "UPDATE tbl_member";
                $sql .= " SET logins = logins + 1,";
                $sql .= " Online = 'Y',";
                $sql .= " time_stamp = now()";
                $sql .= " WHERE user_id = '$uid'";
                $mysqli->query($sql);
            }
        }
    }
    catch (Exception $e)
    {
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    }
?>

PHP:
<?php
    require 'db.php';
    try
    {
        if(!isset($_GET['user_id']) || empty($_GET['user_id']))
        {
            print json_encode("Parameter Error");
            exit;
        }   
        $uid = $mysqli->escape_string($_GET["user_id"]);
        $sql = "SELECT online";
        $sql .= " FROM tbl_member";
        $sql .= " WHERE user_id = '$uid'";
        $result = $mysqli->query($sql);
        $count = $result->num_rows;
        if ($count == 0)
        {
              print json_encode("NotMember");
        }
        else
        {
            $sql = "UPDATE tbl_member";
            $sql .= " SET online = 'N'";
            $sql .= " WHERE user_id = '".$uid."'";
            $mysqli->query($sql);
            print json_encode("LoggedOut");
        }
    }
    catch (Exception $e)
    {
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    }
?>

PHP:
<?php
    require 'db.php';
    try
    {
        $sql = "SELECT user_id, location, online";
        $sql .= " FROM tbl_member";
        $sql .= " ORDER BY id DESC";
        $result = $mysqli->query($sql);
        if ($mysqli->errno)
        {
            header("HTTP/1.1 500 Internal Server Error");
            echo $sql.'\n';
            echo $mysqli->error;
        }
        else
        {
            $rows = array();
            while ($row = $result->fetch_assoc())
            {
                $rows[] = $row;
            }
            print json_encode($rows);
        }
    }
    catch (Exception $e)
    {
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    }
?>

PHP:
<?php
require 'db.php';
try
{
    if (!isset($_GET['Email']) || empty($_GET['Email']) ||
        !isset($_GET['Password1']) || empty($_GET['Password1']) ||
        !isset($_GET['Password2']) || empty($_GET['Password2']))
    {
        print json_encode("Parameter Error");
        exit;
    }
    $email = $mysqli->escape_string($_GET["Email"]);
    $password1 = $mysqli->escape_string($_GET["Password1"]);
    $password2 = $mysqli->escape_string($_GET["Password2"]);
    $sql = "SELECT * FROM tbl_member";
    $sql .= " WHERE email = '".$email."'";
    $sql .= " AND pass_word = '".$password1."'";
    $result = $mysqli->query($sql);
    $count = $result->num_rows;
    if ($count == 0)
    {   
        print json_encode("Not Found");
        echo("<br />User not found or incorrect password");
    }
    else
    {
        $sql = "UPDATE tbl_member";
        $sql .= " SET pass_word = '".$password2."'";
        $sql .= " WHERE email = '".$email."'";
        $sql .= " AND pass_word = '".$password1."'";
        $mysqli->query($sql);   
        print json_encode("Success");
        echo("<br />$email has been updated");
    }
}
catch (Exception $e)
{
    print json_encode("Failed");
    echo '<br />Caught exception: '.$e->getMessage()."\n";
}   
?>

PHP:
<?php
    require 'db.php';
    try
    {
        if(!isset($_GET['Action']) || empty($_GET['Action']))
        {
            print json_encode("Parameter Error");
            exit;
        }
        $action = $mysqli->escape_string($_GET["Action"]);
        switch ($action)
        {
            case "RequestPasswordReset":
                if(!isset($_GET['Mail']) || empty($_GET['Mail']))
                {
                    print json_encode("Parameter Error");
                    exit;
                }       
                $email = $mysqli->escape_string($_GET["Mail"]);
                $sql = "SELECT user_name";
                $sql .= " FROM tbl_member";
                $sql .= " WHERE email = '" . $email . "'";
                $result = $mysqli->query($sql);
                if ($mysqli->errno)
                {
                    print json_encode("MySQL_Error");
                    echo "<br />MySQL_Error: ".$mysqli->error;
                    exit;
                }
                $count = $result->num_rows;
                if ($count == 0)
                {
                    print json_encode("InvalidEmail");
                    exit;
                }
                else
                {
                    // Generate a random code and update to reg_no
                    $verify_code = mt_rand(100000, 999999);
                    $row = $result->fetch_row();
                    $username = $row[0];
                    $sql = "UPDATE tbl_member";
                    $sql .= " SET reg_no = '" . $verify_code . "'";
                    $sql .= " WHERE email = '" . $email . "'";
                    $mysqli->query($sql);
                    if ($mysqli->errno)
                    {
                        print json_encode("MySQL_Error");
                        echo "<br />MySQL_Error: ".$mysqli->error;
                        exit;
                    }
                    // Send email to user to confirm the reset
                    $to      = $email;
                    $subject = 'Request to reset your password';
                    $message = "Hi " . $username . ",\r\n";
                    $message .= "We have received a request from you to reset your password.\r\n";
                    $message .= "If this action is not requested by you,";
                    $message .= " please ignore this email.\r\n";
                    $message .= "Otherwise, click the link below:\r\n";
                    $message .= $server."reset-password.php?Action=ConfirmPasswordReset";
                    $message .= "&Mail=".$email;
                    $message .= "&Code=".$verify_code . "\r\n\r\n";
                    $message .= "If not working, please copy the link to your browser.\r\n\r\n";
                    $message .= "Regards,\r\n";
                    $message .= "Aeric";
                    $message = wordwrap($message, 70, "\r\n");
                    $headers = "From: ".$sender."\r\n";
                    $headers .= "Reply-To: ".$sender."\r\n";
                    $headers .= "X-Mailer: PHP/" . phpversion();
                    mail($to, $subject, $message, $headers);
                    print json_encode("ValidEmail");
                }
            break;
        case "ConfirmPasswordReset":
            if (!isset($_GET['Mail']) || empty($_GET['Mail']) ||
                !isset($_GET['Code']) || empty($_GET['Code']))
            {
                print json_encode("Parameter Error");
                exit;
            }
            $email = $mysqli->escape_string($_GET["Mail"]);
            $code = $mysqli->escape_string($_GET["Code"]);
            $sql = "SELECT user_name";
            $sql .= " FROM tbl_member";
            $sql .= " WHERE email = '" . $email . "'";
            $sql .= " AND reg_no = " . $code;
            $result = $mysqli->query($sql);
            if ($mysqli->errno)
            {
                print json_encode("MySQL_Error");
                echo "<br />MySQL_Error: ".$mysqli->error;
                exit;
            }
            $count = $result->num_rows;
            if ($count == 0)
            {
                print json_encode("InvalidEmailOrCode");
                exit;
            }
            // Generate a default password randomly (e.g. pw1234)
            // You may use other method to generate a more complex password with alphanumeric
            $rand_number = mt_rand(1000, 9999);
            $default = "pw" . $rand_number;
            $row = $result->fetch_row();
            $username = $row[0];
            $sql = "UPDATE tbl_member";
            $sql .= " SET pass_word = '" . $default . "'";
            $sql .= " WHERE email = '" . $email . "'";
            $mysqli->query($sql);
            if ($mysqli->errno)
            {
                print json_encode("MySQL_Error");
                echo "<br />MySQL_Error: ".$mysqli->error;
                exit;
            }
            $to      = $email;
            $subject = 'Your New Password';
            $message = "Hi ".$username.",\r\n";
            $message .= "Your password has been reset.";
            $message .= " Please use your new password to log in.\r\n";
            $message .= "Password: " . $default;
            $message = wordwrap($message, 70, "\r\n");
            $headers = "From: ".$sender."\r\n";
            $headers .= "Reply-To: ".$sender."\r\n";
            $headers .= "X-Mailer: PHP/" . phpversion();
            mail($to, $subject, $message, $headers);
            //print json_encode("PasswordChanged");
            echo "Your password has been reset.<br />Your new password is sent to $email.";
            break;
        default:
            echo("Unauthorized action! Please use the app to reset your password.");
        }
    }
    catch (Exception $e)
    {   
        print json_encode("Failed");
        echo '<br />Caught exception: '.$e->getMessage()."\n";
    }
?>

Code also available in Github

pyhoon/Register-User-B4A

pyhoon/Register-User-PHP
 

Attachments

  • RegisterUser1.1.zip
    36.8 KB · Views: 1,811
  • RegisterUser1.3.zip
    43.3 KB · Views: 848
  • register-user-php.zip
    6 KB · Views: 807
Last edited:

G-ShadoW

Active Member
Licensed User
It's working now, I have switch to signup.php from first post.
Also, register.php is missing?
 

aeric

Expert
Licensed User
(Register) signup.php returns:
1. "Mail"
2. "MailInUse"

(Login) signin.php returns:
1. "Not Found"
2. "Not Activate"
3. User Name

Under Register module, it is unlikely to return "Not Found"

register.php is not used in this example. I use signup.php for register.
 

G-ShadoW

Active Member
Licensed User
Yes, I have modified php, it's working now.
But how to get Online user info :)
It should be something in service module with timer, right ?
 

aeric

Expert
Licensed User
Yes, I have modified php, it's working now.
But how to get Online user info :)
It should be something in service module with timer, right ?
I am not sure how it is done correctly. My idea is the app will periodically create a job request to a php page to update a last_login field in the database then a php page will return a query based on the difference time between current time and the last_login time. Normally Discussion forum list the online user within 5 minutes.
 

G-ShadoW

Active Member
Licensed User
Yes, from php it only count logins

B4X:
$res = mysql_query("UPDATE tbl_member SET logins = logins + 1 WHERE user_id = '$uid'");
    print json_encode ($row["user_name"]);

online status remain N
 

Attachments

  • online.png
    online.png
    3.5 KB · Views: 411

aeric

Expert
Licensed User
Yes, from php it only count logins

B4X:
$res = mysql_query("UPDATE tbl_member SET logins = logins + 1 WHERE user_id = '$uid'");
    print json_encode ($row["user_name"]);

online status remain N
Ya, I didn't implement the Online status. If you want to implement the Online status, I think it is not difficult to do it. As I mentioned, just update the value of last login in the table, in my table I have a time_stamp field which we can use for this purpose.
PHP:
$res = mysql_query("UPDATE tbl_member SET logins = logins + 1, Online = 'Y', time_stamp = now() WHERE user_id = '$uid'");

Then at some point before the above line or by using another php script (maybe we can use connect.php), we can run a query to update all user status to Online = 'N' when Current Time - Time Stamp > 60 seconds.
PHP:
$res = mysql_query("UPDATE tbl_member SET Online = 'N' WHERE now()-time_stamp > 60");
 
Last edited:

G-ShadoW

Active Member
Licensed User
I dont think that will work, there is no logout.
If user exit app, it's stay's as online.
 

aeric

Expert
Licensed User
I dont think that will work, there is no logout.
If user exit app, it's stay's as online.
To force logout, just add in a btnLogout button which create a job to logout. The php web service will execute an SQL which look like below:
PHP:
$res = mysql_query("UPDATE tbl_member SET Online = 'N' WHERE user_id = '$uid'");

If a user exit the app without tapping on the btnLogout button, his status will updated to Online = 'N' when another user login after 60 seconds. You can set the value 60 to 1 seconds if you like.
 

mbayik

Member
Licensed User
hi, thank you for the example, its very useful. im testing it but im taking connection problem..the app says "Connecting to server" and waiting then "error java.net sockettimeoutexpection

my server address is

Sub TestConnection
Dim Connect As HttpJob
Connect.Initialize("Connect", Me)
Connect.Download("http://www.giritliyazilim.net/connect.php")
ProgressDialogShow("Connecting to server...")
End Sub
 

aeric

Expert
Licensed User
hi, thank you for the example, its very useful. im testing it but im taking connection problem..the app says "Connecting to server" and waiting then "error java.net sockettimeoutexpection

my server address is

Sub TestConnection
Dim Connect As HttpJob
Connect.Initialize("Connect", Me)
Connect.Download("http://www.giritliyazilim.net/connect.php")
ProgressDialogShow("Connecting to server...")
End Sub
You can test to access the address using web browser.
 

mbayik

Member
Licensed User
i changed my hosting to free hosting service, its connected. i will continue testing..
 

cwkoomy

Member
Licensed User
hi, i can register and active the user, but when login just show "an error has occured" , sorry newbie dont know which error is it
 

aeric

Expert
Licensed User
hi, i can register and active the user, but when login just show "an error has occured" , sorry newbie dont know which error is it
The free hosting server is facing DDOS attack. I suspect the scripts have been compromised or unable to run properly. Please upload the script to your server. Edit the domain name then test.
 

cwkoomy

Member
Licensed User
The free hosting server is facing DDOS attack. I suspect the scripts have been compromised or unable to run properly. Please upload the script to your server. Edit the domain name then test.
i use pay hosting, i change the signin.php update, then can login, but error " An error has occurred in sub:member_jobdone(java line:421) java.lang.NullPointerException
and Welcome :null , is it change Login.strUserName to strUserID? :)?
 

aeric

Expert
Licensed User
The free hosting server is facing DDOS attack. I suspect the scripts have been compromised or unable to run properly. Please upload the script to your server. Edit the domain name then test.
After I changed the database password, the app can access the server now.
 
Top