Share My Creation (PHP/MySQL/API) User Login App

Web API


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:

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:

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", "[email protected]");
define("NOREPLY_EMAIL", "[email protected]");

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", "[email protected]");
   
    // Email header
    $headers = "From: ".NOREPLY_EMAIL."\r\n";
    $headers .= "Reply-To: ".NOREPLY_EMAIL."\r\n";
    $headers .= "X-Mailer: PHP/".phpversion();

    mail("[email protected]", "test email subject", "The content of the test email.", $headers);
?>

You can also check the SendEmail function in user.php
 

hitesh.k

Member
Hello thanks for api and app i try and it is working . could you sugest me on following points
1: no reply mail is going in spam folder
2 : how to display web page at acc activation link
3 : i like to dig more in to php sugest some ide for php api development
4 : can rest and token could be added in php api
 

aeric

Expert
Licensed User
Longtime User
Hello thanks for api and app i try and it is working . could you sugest me on following points
1: no reply mail is going in spam folder
2 : how to display web page at acc activation link
3 : i like to dig more in to php sugest some ide for php api development
4 : can rest and token could be added in php api
I have missed this message.

1. Not sure. Maybe misconfigured email account settings or delay in email delivery.
2. You can edit the $html inside the activate() in user.php (models)
PHP:
$html = "<h1>Activation</h1>
<p>Your account is now activated!</p>";
echo $html;
3. I don't use any fancy IDE like PHPStorm. Sublime Text, Notepad++ or VSCode is fine.
4. Sure you can extend the project either with plain PHP or use a PHP Web framework.
 

aeric

Expert
Licensed User
Longtime User
Warning: array_key_exists() expects parameter 2 to be array, null given in C:\FTP\api\api\models\user.php on line 82
May be adding a check would fix the warning. Repeat this with other functions which read $Map1.
PHP:
    public function register()
    {
        try
        {           
            $eml = null;
            $pwd = null;
            $name = null;
            $json = file_get_contents("php://input");

            $Map1 = array();   
            $Map1 = json_decode($json, true);
            if (!$Map1)
            {
                $msg_text = "[Invalid JSON Object]";
                $this->LogUser("user/register", "fail", $msg_text, 0);
                $this->ReturnError("Error-No-Value");
                return;
            }           
            if (!array_key_exists("eml", $Map1))
            {
                $msg_text = "[Email Not Set]";
                $this->LogUser("user/register", "fail", $msg_text, 0);
                $this->ReturnError("Error-No-Value");
                return;
            }
 
Top