API接口登录

利用PHP连接MySQL数据库实现了一个简单的用户登录注册及修改密码的API接口,接口为标准的json输出,本地验证通过。功能比较简单,不过也自己摸索了挺长时间,期间各种百度解决了各种不懂。数据库只有name、psd和tel三个字段,接口均设计为POST方式提交。代码基本都有注释比较好理解,下面附上代码。

[php]  view plain  copy
  1. <?php  
  2.     $mysql_server_name="localhost"//数据库服务器名称  
  3.     $mysql_username="root"// 连接数据库用户名  
  4.     $mysql_password="326699"// 连接数据库密码  
  5.     $mysql_database="hello"// 数据库的名字  
  6.       
  7.      // 连接到数据库  
  8.     $conn=mysql_connect($mysql_server_name$mysql_username,  
  9.                         $mysql_password);  
  10.     if(!$conn) {  
  11.         echo "数据库连接失败!".mysql_error;  
  12.     }  
  13.     mysql_select_db($mysql_database$conn);  
  14.       
  15.     //获取url参数  
  16.     $action = isset($_POST['action']) ? $_POST['action'] : '';  
  17.     $name = isset($_POST['name']) ? $_POST['name'] : '';  
  18.     $psd = isset($_POST['psd']) ? $_POST['psd'] : '';  
  19.       
  20.     if($action=='login') {  
  21.         login($name$psd, true);  
  22.     } else if($action=='register') {  
  23.         register($name$psd);  
  24.     } else if($action=='modifyPsd') {  
  25.         modifyPsd($name$psd);  
  26.     } else if($action=='showAll') {  
  27.         showAll();  
  28.     } else {  
  29.         $result = array("result"=>"error_request");  
  30.         $json = json_encode($result);  
  31.         echo $json;  
  32.     }  
  33.       
  34.     close_conn();  
  35.       
  36.     /*用户登录*/  
  37.     function login($name$psd$normal) {  
  38.         global $conn;  
  39.           
  40.         if($conn) {  
  41.             $result = mysql_query("select name,psd from student");  
  42.             $success = false;  
  43.             while($row = mysql_fetch_array($result)) {  
  44.                 if($name == $row['name'] && $psd == $row['psd']) {  
  45.                     $success = true;  
  46.                 }  
  47.             }  
  48.             if($normal) {  
  49.                 $login_result = array('login_result'=>$success);  
  50.                 $json = json_encode($login_result);  
  51.                 echo $json;  
  52.             }  
  53.         }  
  54.         return $success;  
  55.     }  
  56.       
  57.     /*用户注册*/  
  58.     function register($name$psd) {  
  59.         $tel = $_POST['tel'];         
  60.         global $conn;  
  61.           
  62.         if($conn) {  
  63.             //数据库查询  
  64.             $result = mysql_query("select name from student");  
  65.             $exist = false;  
  66.             while($row = mysql_fetch_array($result)) {  
  67.                 if($name == $row['name']) {  
  68.                     //注册失败,用户名已存在;  
  69.                     $exist = true;  
  70.                     $register_result = array("register_result"=>false,"error_code"=>0);  
  71.                     $json = json_encode($register_result);  
  72.                     echo $json;  
  73.                 }  
  74.             }  
  75.               
  76.             //插入数据库           
  77.             if(!$exist) {  
  78.                 $id = mysql_num_rows($result) + 1;  
  79.                 $success = mysql_query("insert into student values('$id', '$name', '$tel', '$psd')");  
  80.                 if($success) {  
  81.                     //注册成功  
  82.                     $register_result = array("register_result"=>$success);  
  83.                     $json = json_encode($register_result);  
  84.                     echo $json;  
  85.                 } else {  
  86.                     //注册失败,数据库插入错误  
  87.                     $register_result = array("register_result"=>$success,"error_code"=>1);  
  88.                     $json = json_encode($register_result);  
  89.                     echo $json;  
  90.                 }  
  91.             }             
  92.         }  
  93.     }  
  94.       
  95.     /*修改登录密码*/  
  96.     function modifyPsd($name$psd) {  
  97.         $newpsd = $_POST['newpsd'];  
  98.         global $conn;  
  99.           
  100.         if($conn) {  
  101.             //用户登录  
  102.             $login_result = login($name$psd, false);  
  103.             //修改密码  
  104.             if($login_result) {  
  105.                 $success = mysql_query("update student set psd='$newpsd' where name='$name'");  
  106.                 if($success) {  
  107.                     //修改成功  
  108.                     $modify_result = array("modify_result"=>$success);  
  109.                     $json = json_encode($modify_result);  
  110.                     echo $json;  
  111.                 } else {  
  112.                     //修改失败,数据库错误  
  113.                     $modify_result = array("modify_result"=>$success,"error_code"=>1);  
  114.                     $json = json_encode($modify_result);  
  115.                     echo $json;  
  116.                 }  
  117.             } else {  
  118.                     //修改失败,登录失败  
  119.                     $modify_result = array("modify_result"=>false,"error_code"=>2);  
  120.                     $json = json_encode($modify_result);  
  121.                     echo $json;  
  122.             }  
  123.         }  
  124.     }  
  125.       
  126.     //显示所有用户  
  127.     function showAll() {  
  128.         global $conn;  
  129.         if($conn) {  
  130.             $result = mysql_query("select * from student");  
  131.             $success = false;  
  132.             $array_data = array();  
  133.   
  134.             $total = mysql_num_rows($result);  
  135.             //$data = array("total"=>$total,"datas"=>array(array("data"=>"123","name"=>"zhugeheng"),  
  136.             //                                                                                   array("data"=>"456","name"=>"zhaodanni")  
  137.             //                                                                      ));  
  138.                                                                                       
  139.             while($row = mysql_fetch_array($result)) {  
  140.                 $array_temp = array("name"=>$row['name'], "tel"=>$row['tel']);  
  141.                 array_push($array_data$array_temp);  
  142.             }  
  143.             $data = array("total"=>$total,"datas"=>$array_data"result"=>true);             
  144.             $json = json_encode($data);  
  145.             echo $json;  
  146.           
  147.         }  
  148.     }  
  149.       
  150.     //关闭连接  
  151.     function close_conn() {  
  152.         global $conn;  
  153.         mysql_close($conn);  
  154.     }  
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值