Share My Creation User Login + MySQL PHP API

Web API

Updates:
For B4J API Server and cross platform client apps,
please check [B4X] User Login App + [B4J] MySQL API Server (Key + Token)
https://www.b4x.com/android/forum/threads/b4x-user-login-app-b4j-mysql-api-server-key-token.126081/

1589568374217.png

1589571389800.png

1589571205757.png


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

03.png

1604679984538.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
Longtime 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:
 

aeric

Expert
Licensed User
Longtime User

tsteward

Well-Known Member
Licensed User
Longtime 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
Longtime 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
Longtime 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
Longtime 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 = '[email protected]';       // 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('[email protected]', 'Mailer');
        $mail->addAddress($user_email, $user_name);
        // $mail->addReplyTo('[email protected]', 'Support');
        // $mail->addCC('[email protected]');
        // $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 = "[email protected]"
    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!
 

IsaacNcho

New Member
J'aimerais savoir pour la connexion d'application B4A et serveur web nécessite une connection internet ?
J'ai eu erreur comme :"failed toi connect toi/My_address_ip_pc(80) from /my_address_ip_phone(port 34534) After 30000ms, Réponse:"
 

aeric

Expert
Licensed User
Longtime User
J'aimerais savoir pour la connexion d'application B4A et serveur web nécessite une connection internet ?
J'ai eu erreur comme :"failed toi connect toi/My_address_ip_pc(80) from /my_address_ip_phone(port 34534) After 30000ms, Réponse:"
No, both the server and B4A can work within your WiFi network or hotspot. Make sure you set the IP address right.
 

winjiadh

Active Member
Licensed User
Longtime User
Hi,aeric
I downloaded your API. Zip file, unzip after uploaded to the VPS, VPS installed Apache Apache2.4.39+ MySQL5.7.26 + php5.6.9,
But the MVC seems installed incorrectly, appeared in the http://test.d99p.cn/views/home/
User (7)
The Password (3)
Click on them, jump to http://test.d99p.cn/views/home/ROOT_URL/user, suggests that cannot find the file, isn't me good MVC is not installed
I set the root directory under the API, set up under the views directory, set in the views/Home directory
Can you teach me?
thank you
 
Last edited:

aeric

Expert
Licensed User
Longtime User
Hi,aeric
I downloaded your API. Zip file, unzip after uploaded to the VPS, VPS installed Nginx1.15.11 + MySQL5.7.26 + php5.6.9,
But the MVC seems installed incorrectly, appeared in the http://test.d99p.cn/views/home/
User (7)
The Password (3)
Click on them, jump to http://test.d99p.cn/views/home/ROOT_URL/user, suggests that cannot find the file, isn't me good MVC is not installed
Can you teach me?
thank you
It seems the variables are not replaced correctly.
What is your www directory structure?

Edit: I just saw you are using Nginx. This API requires Apache web server to work with .htaccess
 

winjiadh

Active Member
Licensed User
Longtime User
I WWW directory is the API.zip files are put forward in the stored in the root directory, as shown
And, I changed the Nginx to Apache
 

Attachments

  • api.jpg
    api.jpg
    84.8 KB · Views: 179

aeric

Expert
Licensed User
Longtime User
I WWW directory is the API.zip files are put forward in the stored in the root directory, as shown
And, I changed the Nginx to Apache
try change the values in config.php

B4X:
// Define URL

// DEVELOPMENT (local server)
// define("ROOT_PATH", "/api/");
// define("ROOT_URL", "http://127.0.0.1:80/api");

// PRODUCTION - using subdomain
define("ROOT_PATH", "/");
define("ROOT_URL", "http://test.d99p.cn/");

define("DEVELOPMENT", "FALSE");
 

winjiadh

Active Member
Licensed User
Longtime User
try change the values in config.php

B4X:
// Define URL

// DEVELOPMENT (local server)
// define("ROOT_PATH", "/api/");
// define("ROOT_URL", "http://127.0.0.1:80/api");

// PRODUCTION - using subdomain
define("ROOT_PATH", "/");
define("ROOT_URL", "http://test.d99p.cn/");

define("DEVELOPMENT", "FALSE");
yes
I changed,but no run,your can look it in http://test.d99p.cn/
 

Attachments

  • api.jpg
    api.jpg
    91.4 KB · Views: 153

winjiadh

Active Member
Licensed User
Longtime User
I saw a double slash when hover the mouse pointer to the URL.

Try:

B4X:
define("ROOT_PATH", "");

or

B4X:
// PRODUCTION - using subdomain
define("ROOT_PATH", "/");
define("ROOT_URL", "http://test.d99p.cn");
yes, you are right
now the web is good
I run b4a app to register user in b4a 11.5 and it's error
<b>Fatal error</b>: Call to a member function prepare() on null in <b>C:\FTP\api\api\classes\Model.php</b> on line <b>35</b><br />
 

aeric

Expert
Licensed User
Longtime User
yes, you are right
now the web is good
I run b4a app to register user in b4a 11.5 and it's error
<b>Fatal error</b>: Call to a member function prepare() on null in <b>C:\FTP\api\api\classes\Model.php</b> on line <b>35</b><br />
I suspect something wrong in the db connection.
Make sure you have set up the MySQL username, password and generate the tables using the sql file.
 
Top