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:

winjiadh

Active Member
Licensed User
Longtime User
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.
I click the http://test.d99p.cn//user
it's [{"Connected":1}] the MySql is connected
but click the http://test.d99p.cn//user/register
display the error

Warning: array_key_exists() expects parameter 2 to be array, null given in C:\FTP\api\api\models\user.php on line 82

Fatal error
: Call to a member function prepare() on null in C:\FTP\api\api\classes\Model.php on line 35
If some places need to set up the PHP can use?
api.jpg
 
Last edited:

aeric

Expert
Licensed User
Longtime User
I click the http://test.d99p.cn//user
it's [{"Connected":1}] the MySql is connected
but click the http://test.d99p.cn//user/register
display the error
/user/connect does not check for DB connection.

You can modify user.php in models directory (NOT controllers directory) as following:
user.php:
    public function connect()
    {
        $success = array("Connected" => 1);
        $failed = array("Connected" => 0);
        if ($this->open())
        {
            print json_encode(array($success));
        }
        else
        {
            print json_encode(array($failed));
        }
    }

1649928248627.png
 
Last edited:

aeric

Expert
Licensed User
Longtime User
You cannot just click on the URL as it will only make a GET call. To make a POST call, you need to use something like Postman to do the test.

1649928717138.png
 

winjiadh

Active Member
Licensed User
Longtime User
Try use adminer.php to login to your MySQL.
You can download from https://www.adminer.org
/user/connect does not check for DB connection.

You can modify user.php in models directory (NOT controllers directory) as following:
user.php:
    public function connect()
    {
        $success = array("Connected" => 1);
        $failed = array("Connected" => 0);
        if ($this->open())
        {
            print json_encode(array($success));
        }
        else
        {
            print json_encode(array($failed));
        }
    }

View attachment 127907
now if i use the root to login,the api is all right
but registed succesed no email send to the mailbox
 

aeric

Expert
Licensed User
Longtime User
no email send to the mailbox
You need to change the email settings in config.php

PHP:
// Define Email Sender
define("ADMIN_EMAIL", "mailer@computerise.my");
define("NOREPLY_EMAIL", "no-reply@computerise.my");

ADMIN_EMAIL = the email you want to receive message if a new user register. It can be any email such as google, yahoo, qq.com, 163.com
NOREPLY_EMAIL = the email account set up in your hosting server for sending out email

You can do a test with this php script:

PHP:
<?php
    define("NOREPLY_EMAIL", "admin@d99p.cn");
   
    // Email header
    $headers = "From: ".NOREPLY_EMAIL."\r\n";
    $headers .= "Reply-To: ".NOREPLY_EMAIL."\r\n";
    $headers .= "X-Mailer: PHP/".phpversion();

    mail("someone@qq.com", "test email subject", "The content of the test email.", $headers);
?>

You can also check the SendEmail function in user.php
 
Top