1-Develop Shared APP (Build Guide) - Build your own server quickly

The APP installation package download link:

http://www.mnif.cn/appapk/IotDevelopmentVersion/20190730/app-debug.apk

Or scan for QR code download

 

Access to APP source code: (please read the baby's instructions)

This APP is built on basic, upgrade, data and security chapters. You can only understand the source code after you have learned all these chapters!

https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.70471debVGb9G8&ft=t&id=569295486025

 

If a user gets the source code and wants to quickly apply it to his or her own server, see this series of articles

 

First, set up a configuration MQTT server

1. In the basics section, install and configure an MQTT server

 

 

2. In the Security section, configure secure connections for MQTT

https://www.cnblogs.com/yangfengwu/category/1451836.html

 

 

2. Install Configuration Database

1. In the data section, install the database

https://www.cnblogs.com/yangfengwu/category/1410242.html

 

In the cloud database, create a table named register with the database name login. The field names in the table are as follows

 

 

 

 

Third, set up and configure Apache server

1. In the upgrade section, install and configure the Apache server

https://www.cnblogs.com/yangfengwu/category/1383497.html

 

2. In the WeChat Applet,

https://www.cnblogs.com/yangfengwu/category/1462689.html

 

 

 

4. Cloud Server Configuration Resolves Registered php Programs

1. Create a folder named LoginAndRegistration at the root of the page

 

 

 ChangePassword.php

<?php

//http://IPaddress/LoginAndRegistration/ChangePassword.php?Phone=13275429560&Password=25
//https://domain name/LoginAndRegistration/ChangePassword.php?Phone=13275429560&Password=25
//PHP Specifies that all variables must be preceded by a $
//To be clear, PHP Of echo All of them http Data returned

$servername = "localhost";//Link local database
$username = "root";//root account
$password = "qwer123456";//Password, according to your changes
$dbname = "login";//Linked database name
$TableName = "register";//Name of linked table


function groupSelect($from, $where1, $condition1,$value1, $condition,$where2, $condition2, $value2) {
    $sql = "select *from ".$from." where ".$where1.$condition1."'".$value1."'".$condition ." ".$where2.$condition2."'".$value2."'";
    return  $sql;
}
function groupSelect1($from, $where1, $condition1,$value1) {
    $sql = "select *from ".$from." where ".$where1.$condition1."'".$value1."'";
    return  $sql;
}


try {
    $LoginPhone = $_GET["Phone"];//User Submitted Name
    $Loginpassword  = $_GET["Password"];//User Submitted Password

    try {
        $conn = new PDO("mysql:host=$servername;port=3306;dbname=$dbname", $username, $password);//Link Database
        //echo "PDO Of API Connection Successful";

        // Set up PDO Error pattern is exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $Flage = false;

        $sql = groupSelect1($TableName, "phone","=",$LoginPhone);//Query if there is this mobile number

        $result = $conn->query($sql);//Send out SQL Statement and receive database returns

        if ($result && $result->rowCount()) {//Query to data
            $Flage = true;
        }

        if($Flage == true){
            $Flage = false;
            $sql = "UPDATE ".$TableName." SET password = "."'".$Loginpassword ."'"." WHERE phone=" .$LoginPhone;//To update
            $count  =  $conn->exec($sql); //Return the number of rows affected
            if($count!=0){
               echo "OK";//Return
            }else{
               echo "errorCode:NoChange";//Return
            }
        }
        else{
            echo "errorCode:NoUserName";//No users
        }
        $conn=null;//Close
    }
    catch(PDOException $e){
        echo $e->getMessage();
        echo "errorCode:DataBase";//Return
    }
}
catch(PDOException $e)
{
    echo $e->getMessage();
    echo "errorCode:ReadData";//Return
}

 

 

 login.php

<?php

//http://IPaddress/LoginAndRegistration/login.php?Phone=13275429560&Password=25
//PHP Specifies that all variables must be preceded by a $
//To be clear, PHP Of echo All of them http Data returned

$servername = "localhost";
$username = "root";
$password = "qq946029359";//According to your own modifications
$dbname = "login";//Linked database name
$TableName = "register";//Name of linked table


function groupSelect($from, $where1, $condition1,$value1, $condition,$where2, $condition2, $value2) {
    $sql = "select *from ".$from." where ".$where1.$condition1."'".$value1."'".$condition ." ".$where2.$condition2."'".$value2."'";
    return  $sql;
}
function groupSelect1($from, $where1, $condition1,$value1) {
    $sql = "select *from ".$from." where ".$where1.$condition1."'".$value1."'";
    return  $sql;
}


try {
    $LoginPhone = $_GET["Phone"];//User Submitted Name
    $Loginpassword  = $_GET["Password"];//User Submitted Password
    
    try {
        $conn = new PDO("mysql:host=$servername;port=3306;dbname=$dbname", $username, $password);//Link Database
        //echo "PDO Of API Connection Successful"; 
        
        // Set up PDO Error pattern is exception 
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

        $Flage = false;

        $sql = groupSelect1($TableName, "phone","=",$LoginPhone);//Query if there is this mobile number

        $result = $conn->query($sql);//Send out SQL Statement and receive database returns

        if ($result && $result->rowCount()) {//Query to data
            $Flage = true;
        }

        if($Flage == true){
            $Flage = false;
            $sql = groupSelect($TableName, "phone","=",$LoginPhone,"and","password","=",$Loginpassword);//combination SQL Sentence
            $result = $conn->query($sql);//Send out SQL Statement and receive database returns
            if ($result && $result->rowCount()) {//Query to data
                //Actually, you just need to query it  $Flage = true;
                while($row = $result->fetch()) {//Loop Read Data
                    $Flage = true;//User name and password are correct
                    //echo $row['phone'].$row['password'];//print data
                    break;
                }
            }
            if($Flage == true)
            {
              echo "OK";//Return
            }
            else
            {
              echo "errorCode:Password";//Password error
            }
        }
        else{
            echo "errorCode:NoUserName";//No users
        }
        $conn=null;//Close
    }
    catch(PDOException $e){
        //echo $e->getMessage();
        echo "errorCode:DataBase";//Return
    }
}
catch(PDOException $e)
{
    //echo $e->getMessage();
    echo "errorCode:ReadData";//Return
}

 

 Register.php

<?php
//Register or retrieve password
//http://IPaddress/LoginAndRegistration/Register.php?TAG=Register&Phone=13275429560&Password=25  //register
//http://IPaddress/LoginAndRegistration/Register.php?TAG=RePassword&Phone=13275429560&Password=25  //Retrieve password

//https://domain name/LoginAndRegistration/Register.php?TAG=Register&Phone=13275429560&Password=25  //register
//https://domain name/LoginAndRegistration/Register.php?TAG=RePassword&Phone=13275429560&Password=25  //Retrieve password
//PHP Specifies that all variables must be preceded by a $
//To be clear, PHP Of echo All of them http Data returned

$servername = "localhost";
$username = "root";
$password = "qwer123456";//According to your own modifications
$dbname = "login";//Linked database name
$TableName = "register";//Name of linked table

function groupSelect($from, $where1, $condition1,$value1, $condition,$where2, $condition2, $value2) {
    $sql = "select *from ".$from." where ".$where1.$condition1."'".$value1."'".$condition ." ".$where2.$condition2."'".$value2."'";
    return  $sql;
}
function groupSelect1($from, $where1, $condition1,$value1) {
    $sql = "select *from ".$from." where ".$where1.$condition1."'".$value1."'";
    return  $sql;
}
function groupDelete($from, $where, $condition,$value) {
    $sql = "delete from ".$from." where ".$where.$condition."'".$value."'";
    return  $sql;
}

function groupInsert($from,&$option,&$value) {
    $Option = $option[0];
    $Value = "'".$value[0];
    $bl = false;

    foreach($option as $va){
        if ($bl == true){ $Option = $Option.",".$va;}
        else{$bl = true;}
    }
    $bl = false;
    foreach($value as $va){
        if ($bl == true) $Value = $Value."'".","."'".$va;
        else $bl = true;
    }
    $Value=$Value ."'";
    $sql = "insert into ".$from ."("  .$Option .")". " values(". $Value  .")";
    return  $sql;
}
try {
    $LoginTAG = $_GET["TAG"];//Register or retrieve password
    $LoginPhone = $_GET["Phone"];//User Submitted Name
    $Loginpassword  = $_GET["Password"];//User Submitted Password

    try {
        $conn = new PDO("mysql:host=$servername;port=3306;dbname=$dbname", $username, $password);//Link Database
        //echo "PDO Of API Connection Successful";
        // Set up PDO Error pattern is exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $Flage = false;
        $sql = groupSelect1($TableName, "phone","=",$LoginPhone);//Query if there is this mobile number
        $result = $conn->query($sql);//Send out SQL Statement and receive database returns
        if ($result && $result->rowCount()) {//Query to data
            $Flage = true;
        }

        if($Flage == true){//Have this user
            $Flage = false;
            if($LoginTAG == "Register"){//If registered
                echo "errorCode:UserAlreadyExists";//Return user already exists
            }else if ($LoginTAG == "RePassword"){//If the password is retrieved
                //notes:User retrieves password I recommend clearing all device information bound by this user first
                /*Write a function to delete all information about the user's bound device*/
                $sql = groupDelete($TableName,"phone","=",$LoginPhone);//Delete the information for this phone number
                $count  =  $conn->exec($sql); //Return the number of rows affected
                if($count!=0){//Deleted data
                    $option=array("phone","password");
                    $value=array($LoginPhone,$Loginpassword);
                    $sql = groupInsert($TableName,$option,$value);
                    $count  =  $conn->exec($sql); //Return the number of rows affected
                    if ($count !=0){//Successful insertion of data
                        echo "OK";//Password Reset Successful
                    }else{
                        echo "errorCode:DataBase";//Failed to reset password,Access database exception
                    }
                }else{
                    echo "errorCode:DeleteUser";//Delete User Information Exception
                }
            }
        }
        else{//No such user
            if($LoginTAG == "Register"){//If registered
                $option=array("phone","password");
                $value=array($LoginPhone,$Loginpassword);
                $sql = groupInsert($TableName,$option,$value);
                $count  =  $conn->exec($sql); //Return the number of rows affected
                if ($count !=0){//Successful insertion of data
                    echo "OK";//login was successful
                }else{
                    echo "errorCode:DataBase";//login has failed,Access database exception
                }
            }else if ($LoginTAG == "RePassword"){//If the password is retrieved
                echo "errorCode:NoUserName";//No users
            }
        }
        $conn=null;//Close
    }
    catch(PDOException $e){
        //echo $e->getMessage();
        echo "errorCode:DataBase";//Return
    }
}
catch(PDOException $e)
{
    //echo $e->getMessage();
    echo "errorCode:ReadData";//Return
}

 

Fifth, modify the address information of APP connections

1, modify the IP address of the registered login connection

 

 

 

2, Modify the information of the connected MQTT

 

 

3, the update configuration of APP (just place the update files and the updated installation packages where they can be accessed and downloaded from http or https)

1, Modify access to update files, according to your own modifications

 

 

The content of the updated file accessed

{
VerCode:5,
VerName:1.2.1,
Address:"http://www.mnif.cn/appapk/IotDevelopmentVersion/20190730/app-debug.apk",
Details:
"1,Add Customization MQTT Connect,Support TCP,SSL;
2,Binding devices can customize subscribed topics
3,Supports single switch control
4,Optimized communication monitor program
"
}

Note: The entire file content is in JSON format

VerCode:5,
VerName:1.2.1,
After each APP installation package upload, please modify this to be consistent within APP

 


Address:"http://www.mnif.cn/appapk/IotDevelopmentVersion/20190730/app-debug.apk",
This is the address information of the APP installation package, please modify it yourself

Note: My way is to first access the contents of the updateinfo.txt file and then compare the version information.
Prompt if there is a new version
1, add custom MQTT connection, support TCP,SSL;
2, Binding devices can customize subscribed topics
3, Support single switch control
4, Optimize the communication monitoring program

After users click Update, APP visits http://www.mnif.cn/app apk/IotDevelopmentVersion/20190730/app-debug.apk
Download the installation package, download it, and invoke the installation.
 


Tags: PHP SQL Database PDO

Posted on Tue, 30 Jul 2019 14:53:02 -0400 by alohatofu