php登錄注冊(cè)接口

了解JSON

JSON 指的是 JavaScript 對(duì)象表示法(JavaScript Object Notation)
JSON 是輕量級(jí)的文本數(shù)據(jù)交換格式
JSON 獨(dú)立于語(yǔ)言
JSON 具有自我描述性,更易理解

JSON 是存儲(chǔ)和交換文本信息的語(yǔ)法。類似 XML。

JSON 比 XML 更、更,更易解析。

格式轉(zhuǎn)化

學(xué)習(xí)了php的基本語(yǔ)法的你們肯定知道數(shù)組Array這個(gè)基本數(shù)據(jù)啦,因?yàn)槲覀兦岸孙@示出的數(shù)據(jù)庫(kù)數(shù)據(jù)一般由數(shù)組表示的,那么基本數(shù)據(jù)數(shù)組和JSON之間要怎么轉(zhuǎn)換呢?

json_encode():將基本數(shù)據(jù)數(shù)組轉(zhuǎn)換為json格式
json_deconde():將json格式轉(zhuǎn)化為基本數(shù)據(jù)數(shù)組格式

ps:從后臺(tái)返回?cái)?shù)據(jù)(echo $json)到前臺(tái),獲得數(shù)據(jù)時(shí)候,
一般同學(xué)都是選擇用eval()函數(shù)來(lái)進(jìn)行格式轉(zhuǎn)化,
考慮到對(duì)項(xiàng)目安全性問(wèn)題,小編個(gè)人建議大家少用,
而選擇用getJSON()函數(shù)。它具有相同的功能,而且安全性要強(qiáng)一些。

常見(jiàn)的json格式:

image.png

json四原則:

“:”:數(shù)據(jù)在名稱/值對(duì)中
“,”:數(shù)據(jù)由逗號(hào)分隔
“{ }”:花括號(hào)保存對(duì)象
“[ ]”:方括號(hào)保存數(shù)組

寫(xiě)php接口

個(gè)人寫(xiě)接口的時(shí)間不長(zhǎng),從網(wǎng)上資料及視頻教程中得出經(jīng)驗(yàn):寫(xiě)簡(jiǎn)單接口,就相當(dāng)寫(xiě)MVC中的MC,只寫(xiě)邏輯代碼層,把功能用函數(shù)封裝起來(lái),到時(shí)候include,直接調(diào)用就好了。

php接口知識(shí)

<?php
    interface People{
        public function say();
        public function dance();
    }

    interface Animal{
        public function bite();
    }

    class Man implements People{
        public function say(){
            echo 'i can say';
        }    

        public function dance(){
            echo 'i can dance';
        }
    }

    class Woman implements People{
        public function say(){
            echo 'i can say girl voice';
        }    

        public function dance(){
            echo 'i can dance like a goose';
        }
    }


    class Bird implements Animal{
        public function bite(){
            echo 'i can bite';
        }
    }

    class Hybreed implements People,Animal{
        public function say(){
            echo 'i can say';
        }

        public function dance(){
            echo 'i can dance';
        }

        public function bite(){
            echo 'i can bite';
        }
    }

    $hybreed = new Hybreed;
    echo $hybreed->say()."\r\n";
    echo $hybreed->dance()."\r\n";
    echo $hybreed->bite()."\r\n";

接口存在的意義就是實(shí)現(xiàn)“多重繼承”,準(zhǔn)確的來(lái)說(shuō)應(yīng)該就做“多重實(shí)現(xiàn)“,因?yàn)橐粋€(gè)php類只能有一個(gè)父類,而一個(gè)類卻可以實(shí)現(xiàn)多個(gè)接口,就像大一學(xué)C++時(shí),上面代碼interface.php中的Hybreed類,即實(shí)現(xiàn)了people接口,又實(shí)現(xiàn)了Animal接口,而通過(guò)這種多重繼承,最終的$hybreed即獲得了人類的say和dance的方法,又獲得了動(dòng)物的bite方法。而Man類和Woman類分別實(shí)現(xiàn)People的接口,采用不同的內(nèi)容去重寫(xiě)了say和dance方法,正是一種多態(tài)的體現(xiàn)。

登錄注冊(cè)api接口實(shí)例

<?php (如遇手機(jī)顯示不全,可右滑)

    //數(shù)據(jù)庫(kù)連接部分--開(kāi)始
    $mysql_server_name="localhost"; //數(shù)據(jù)庫(kù)服務(wù)器名稱
    $mysql_username="root"; // 連接數(shù)據(jù)庫(kù)用戶名
    $mysql_password=""; // 連接數(shù)據(jù)庫(kù)密碼
    $mysql_database="hello"; // 數(shù)據(jù)庫(kù)的名字
    
     // 連接到數(shù)據(jù)庫(kù)
    $conn=mysql_connect($mysql_server_name, $mysql_username,$mysql_password);
    if(!$conn) {
        echo "數(shù)據(jù)庫(kù)連接失?。?.mysql_error;
    }
    mysql_select_db($mysql_database, $conn);
    //數(shù)據(jù)庫(kù)部分--結(jié)束

    /* 登錄、注冊(cè)、修改個(gè)人信息、顯示用戶接口‘菜單’代碼--開(kāi)始*/

    //獲取url參數(shù)
    $action = isset($_POST['action']) ? $_POST['action'] : '';
    $name = isset($_POST['name']) ? $_POST['name'] : '';
    $psd = isset($_POST['psd']) ? $_POST['psd'] : '';
    
    if($action=='login') {
        login($name, $psd, true);
    } else if($action=='register') {
        register($name, $psd);
    } else if($action=='modifyPsd') {
        modifyPsd($name, $psd);
    } else if($action=='showAll') {
        showAll();
    } else {
        $result = array("result"=>"error_request");//返回一個(gè)錯(cuò)誤提示
        $json = json_encode($result);
        echo $json;
    
    close_conn();
    /*‘菜單’代碼--結(jié)束*/

    /*用戶登錄*/
    function login($name, $psd, $normal) {
        global $conn;
        if($conn) {
            $result = mysql_query("select name,psd from student");
            $success = false;
            while($row = mysql_fetch_array($result)) {
                if($name == $row['name'] && $psd == $row['psd']) {
                    $success = true;
                }
            }
            if($normal) {
                $login_result = array('login_result'=>$success);
                $json = json_encode($login_result);
                echo $json;
            }
        }
        return $success;
    }
    
    /*用戶注冊(cè)*/
    function register($name, $psd) {
        $tel = $_POST['tel'];       
        global $conn;
        
        if($conn) {
            //數(shù)據(jù)庫(kù)查詢
            $result = mysql_query("select name from student");
            $exist = false;
            while($row = mysql_fetch_array($result)) {
                if($name == $row['name']) {
                    //注冊(cè)失敗,用戶名已存在;
                    $exist = true;
                    $register_result = array("register_result"=>false,"error_code"=>0);
                    $json = json_encode($register_result);
                    echo $json;
                }
            }
            //插入數(shù)據(jù)庫(kù)         
            if(!$exist) {
                $id = mysql_num_rows($result) + 1;
                $success = mysql_query("insert into student values('$id', '$name', '$tel', '$psd')");
                if($success) {
                    //注冊(cè)成功
                    $register_result = array("register_result"=>$success);
                    $json = json_encode($register_result);
                    echo $json;
                } else {
                    //注冊(cè)失敗,數(shù)據(jù)庫(kù)插入錯(cuò)誤
                    $register_result = array("register_result"=>$success,"error_code"=>1);
                    $json = json_encode($register_result);
                    echo $json;
                }
            }           
        }
    }
    
    /*修改登錄密碼*/
    function modifyPsd($name, $psd) {
        $newpsd = $_POST['newpsd'];
        global $conn;
        
        if($conn) {
            //用戶登錄
            $login_result = login($name, $psd, false);
            //修改密碼
            if($login_result) {
                $success = mysql_query("update student set psd='$newpsd' where name='$name'");
                if($success) {
                    //修改成功
                    $modify_result = array("modify_result"=>$success);
                    $json = json_encode($modify_result);
                    echo $json;
                } else {
                    //修改失敗,數(shù)據(jù)庫(kù)錯(cuò)誤
                    $modify_result = array("modify_result"=>$success,"error_code"=>1);
                    $json = json_encode($modify_result);
                    echo $json;
                }
            } else {
                    //修改失敗,登錄失敗
                    $modify_result = array("modify_result"=>false,"error_code"=>2);
                    $json = json_encode($modify_result);
                    echo $json;
            }
        }
    }
    
    //顯示所有用戶
    function showAll() {
        global $conn;
        if($conn) {
            $result = mysql_query("select * from student");
            $success = false;
            $array_data = array();

            $total = mysql_num_rows($result);
            //$data = array("total"=>$total,"datas"=>array(array("data"=>"123","name"=>"zhugeheng"),
            //                                                                                   array("data"=>"456","name"=>"zhaodanni")
            //                                                                      ));                                         
            while($row = mysql_fetch_array($result)) {
                $array_temp = array("name"=>$row['name'], "tel"=>$row['tel']);
                array_push($array_data, $array_temp);
            }
            $data = array("total"=>$total,"datas"=>$array_data, "result"=>true);            
            $json = json_encode($data);
            echo $json;
        
        }
    }
    
    //關(guān)閉連接
    function close_conn() {
        global $conn;
        mysql_close($conn);
    }
      尊重原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處:[http://blog.csdn.net/zhugehengheng/article/details/44645287](http://blog.csdn.net/zhugehengheng/article/details/44645287)**

?>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評(píng)論 19 139
  • 介紹 a library for composing asynchronous and event-based p...
    王培921223閱讀 469評(píng)論 0 0
  • 這一天和以往并沒(méi)有很明顯的區(qū)別。 但當(dāng)太陽(yáng)的光輝落下的最后一剎那,總能感覺(jué)到有些東西不同了。 總是很喜歡看離經(jīng)叛道...
    shorpsword閱讀 135評(píng)論 0 1
  • 花開(kāi)的時(shí)候 聽(tīng)見(jiàn)你輕嘆息 韶華易逝 今日是明日殤的序曲 花落的時(shí)候 聽(tīng)見(jiàn)你輕嘆息 容顏已老 微風(fēng)是帶我離去的翅膀
    南方的茶閱讀 225評(píng)論 0 2
  • 你用Ruby干什么?你是如何參與Ruby社區(qū)的? Dave:在過(guò)去的8年中,我用Ruby做所有的事!我們整個(gè)的線上...
    飛將軍閱讀 3,941評(píng)論 1 5

友情鏈接更多精彩內(nèi)容