Share My Creation User Login + MySQL PHP API

Update: For B4J API Server and cross platform client apps, please check [B4X] User Login App + [B4J] MySQL API Server (Key + Token)

Android App
01.png
02.png
05.png
06.png

03.png

1604679984538.png

Web API
1589568374217.png

1589571389800.png

1589571205757.png


This is the 3rd version of login example I share in this forum.

The first version I shared when I started learning B4A is just a very simple login app.
Http Login example using HttpUtils2

Later, I have shared a tutorial in Code Snippet title:
Register User example using OKHttpUtils2

Both of the samples I shared are using very simple PHP with mysql_query function. I have later updated the 2nd sample with mysqli since mysql functions are deprecated in PHP.

Recently I am thinking to update the sample project with what I have learned in this few years. I have seen some members here are still using mysqli but I want to introduce the use of PDO in PHP. The sample I shared here is just intended for learning purpose. Anyone are welcome to extend it to become a production ready and more powerful app. For example, In my real app, I have a function to upload profile photo. I remove the feature to leave it to members here to find out how to do it. The answer is in this forum and the Internet.

In my previous code snippet, I use 6 digit security code but in this latest version, I have used a 32 characters MD5 string for the code in email confirmation.

The PHP app I share here is a cut down version of PHP backend I use in production. It is based on MVC concept where it provides a web application front-end and includes php session and some other security. I think I don't need to share much on the "View" part of the MVC. I just modified it to become the "API" to consume by the B4A app. However, I use the View part to show a documentation on how to use the API as you can see in the screenshots. For the "Model" part, I only include "User" and "Password". It is easy to extend to more models. For example, I could have add "Driver" and "Order" model for the e-hailing app or "Student" and "Courses" models to my e-learning app.
 
Last edited:

aeric

Expert
Licensed User
Very Nice.
That's what I'm also developing right now, only I don't want to use PHP, but everything with B4J.
Security plays a big role.
Please check this:
 

tsteward

Well-Known Member
Licensed User
I can see there are 2 slashes before user in the url. Try edit the config file so it shows only 1 slash.
I removed the trailing slash in the config file for "root_url"
PHP:
// PRODUCTION - using subdomain
define("ROOT_PATH", "/api/");
define("ROOT_URL", "https://locksdownunder.com");

define("DEVELOPMENT", "FALSE");
That gave this link and this error:
Your code is installed in locksdownunder.com/api/

1609757111009.png
 

aeric

Expert
Licensed User
There are 2 cases. Do you (1) create a subdomain called "api" or (2) a sub folder in the root?
If you use cPanel to create subdomain, then the folder "api" will become your document root. In this case:
PHP:
// PRODUCTION - using subdomain
define("ROOT_PATH", "/");
define("ROOT_URL", "https://api.locksdownunder.com/");

If you just copy the folder into the root, try:
PHP:
// PRODUCTION - using folder inside root
define("ROOT_PATH", "/api/");
define("ROOT_URL", "https://locksdownunder.com/api/");
Very likely the second settings are the right for you.
 
Last edited:

tsteward

Well-Known Member
Licensed User
Okay seems to be all sorted out thank you, and thank you for all your hard work here.
Is there a way to add phpmailer to your code so I can send emails from my gmail account?
 

aeric

Expert
Licensed User
Okay seems to be all sorted out thank you, and thank you for all your hard work here.
Is there a way to add phpmailer to your code so I can send emails from my gmail account?
Yes. You can replace the SendEmail function to following:
PHP:
    private function SendEmail($user_name, $user_email, $activation_code)
    {
        require 'PHPMailerAutoload.php';

        $mail = new PHPMailer;

        // $mail->SMTPDebug = 3;                               // Enable verbose debug output

        $mail->isSMTP();                                      // Set mailer to use SMTP
        $mail->Host = 'smtp.gmail.com';                          // Specify main SMTP servers
        $mail->SMTPAuth = true;                               // Enable SMTP authentication
        $mail->Username = 'my_gmail_account@gmail.com';       // SMTP username
        $mail->Password = 'xxxxxxxx';                         // SMTP password
        $mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
        $mail->Port = 587;                                    // TCP port to connect to

        $mail->setFrom('my_gmail_account@gmail.com', 'Mailer');
        $mail->addAddress($user_email, $user_name);
        // $mail->addReplyTo('xxxxxx@example.com', 'Support');
        // $mail->addCC('cccccc@example.com');
        // $mail->addBCC(ADMIN_EMAIL); // copy to Admin

        $mail->isHTML(true);                                  // Set email format to HTML

        $body = "Hi ".$user_name.",<br/>";
        $body .= "Please click on this link to finish the registration process:<br/>";
        $body .= ROOT_URL."/user/activate/".$activation_code;    

        $mail->Subject = APP_TRADEMARK;
        $mail->Body    = $body;
        $mail->AltBody = "Hi $user_name,\r\n";
        $mail->AltBody .= "Please click on this link to finish the registration process:\r\n";
        $mail->AltBody .= ROOT_URL."/user/activate/".$activation_code;

        if(!$mail->send()) {
            //echo 'Message could not be sent.';
            $msg_text = "[Mailer Error] ".$mail->ErrorInfo;
            $this->LogError("user/activate", $msg_text);
        } else {
            //echo 'Message has been sent';
            $msg_text = $eml;
            $this->LogUser("user/activate", "success", $msg_text, 0);
        }
    }

I get this guide: https://www.pepipost.com/tutorials/send-an-email-via-gmail-smtp-server-using-php/
 
Last edited:

Daniel44

Active Member
Licensed User
B4A User Login App (App.zip)
Note: In Starter module, under Process_Globals, you can change the values to your own values.
B4X:
    'Dim strURL As String = "http://172.20.10.6:8000/api/"                         ' Local / Development (Hotspot)
    Dim strURL As String= "https://api.computerise.my/"                            ' Remote / Production
    Dim DEMO_EMAIL As String = "myemail@gmail.com"
    Dim DEMO_PASSWORD As String = "mydemopass"
    Dim DEV As Boolean = True

PHP Web API (api.zip)
SQL script to create the database is included in the zip file.
Note: In my shared hosting, I need to rename folders in views to start with capital letter. Example: wwwroot/api/views/Home.

View attachment 94217
Thank you for sharing!
 
Top