PHP實(shí)現(xiàn)的注冊,登錄及查詢用戶資料功能API接口示例
本文實(shí)例講述了PHP實(shí)現(xiàn)的注冊,登錄及查詢用戶資料功能API接口。分享給大家供大家參考,具體如下:
服務(wù)端
<?php
require 'conn.php';
header('Content-Type:text/html;charset=utf-8');
$action = $_GET['action'];
switch ($action) {
//注冊會員
case"adduserinfo";
$username = lib_replace_end_tag(trim($_GET['username']));
$password2 = lib_replace_end_tag(trim($_GET['userpassword']));
$password = md5("$password2" . ALL_PS);
$email = lib_replace_end_tag(trim($_GET['email']));
if ($username == '' || $password2 == '' || $password == '') {
$res = urlencode("參數(shù)有誤");
exit(json_encode($res)); //有空信息
}
$sql = "select username from `member` where username='$username'";
$query = mysql_query($sql, $conn);
$count = mysql_num_rows($query);
if ($count > 0) {
exit(json_encode(1)); //返回1表示注冊失敗
} else {
$addsql = "insert into `member` (username,password,email) values ('$username','$password','$email')";
mysql_query($addsql);
exit(json_encode(0)); //返回0表示注冊成功
}
break;
//查詢用戶信息
case"selectuserinfo";
$username = lib_replace_end_tag($_GET['username']);
$sql = "select id,username,nickname,mobile from `member` where username='$username'";
$query = mysql_query($sql, $conn);
$row = mysql_fetch_array($query);
foreach ($row as $key => $v) {
$res[$key] = urlencode($v);
}
exit(json_encode($res));
break;
//會員登錄
case"userlogin";
$username = lib_replace_end_tag($_GET['username']);
$password2 = lib_replace_end_tag(trim($_GET['userpassword']));
$password = md5("$password2" . ALL_PS);
$sqluser = "select id,username,password from `member` where username='" . $username . "' and password='" . $password . "'";
$queryuser = mysql_query($sqluser);
$rowuser = mysql_fetch_array($queryuser);
if ($rowuser && is_array($rowuser) && !empty($rowuser)) {
if ($rowuser['username'] == $username && $rowuser['password'] == $password) {
if ($rowuser['password'] == $password) {
$res = urlencode("登錄成功");
exit(json_encode($res));
} else {
$res = urlencode("密碼錯誤");
exit(json_encode($res));
}
} else {
$res = urlencode("用戶名不存在");
exit(json_encode($res));
}
} else {
$res = urlencode("用戶名密碼錯誤");
exit(json_encode($res));
}
/*
* 0:表示登錄成功,1:表示密碼錯誤,2:用戶名不存在,3:用戶名密碼錯誤
*/
break;
default:
exit(json_encode(error));
}
?>
客戶端例子:
<?php
header('Content-Type:text/html;charset=utf-8'); //避免輸出亂碼
function httpPost($url, $parms) {
$url = $url . $parms;
if (($ch = curl_init($url)) == false) {
throw new Exception(sprintf("curl_init error for url %s.", $url));
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if (is_array($parms)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data;'));
}
$postResult = @curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($postResult === false || $http_code != 200 || curl_errno($ch)) {
$error = curl_error($ch);
curl_close($ch);
throw new Exception("HTTP POST FAILED:$error");
} else {
// $postResult=str_replace("\xEF\xBB\xBF", '', $postResult);
switch (curl_getinfo($ch, CURLINFO_CONTENT_TYPE)) {
case 'application/json':
$postResult = json_decode($postResult);
break;
}
curl_close($ch);
return $postResult;
}
}
$postUrl = "http://pujia.test.com/api/server.php";
$p=$_GET['p'];
if ($p =="selectuserinfo") {
$username = $_GET['username'];
$parms = "?action=selectuserinfo&username=" . $username . "";
} elseif ($p =="adduserinfo") {
$username = $_GET['username'];
$userpassword = $_GET['userpassword'];
$parms = "?action=adduserinfo&username=" . $username . "&userpassword=" . $userpassword . "";
} elseif ($p =="userlogin") {
$username = $_GET['username'];
$userpassword = $_GET['userpassword'];
$parms = "?action=userlogin&username=" . $username . "&userpassword=" . $userpassword . "";
}
$res = httpPost($postUrl, $parms); //$parms
$res = json_decode($res);
print_r(urldecode(json_encode($res)));
?>
注:代碼中的lib_replace_end_tag函數(shù)為自定義字符串過濾函數(shù),具體可參考:淺析php過濾html字符串,防止SQL注入的方法
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+MySQL會員系統(tǒng)開發(fā)專題》、《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)的簡單中文驗(yàn)證碼功能示例
這篇文章主要介紹了php實(shí)現(xiàn)的簡單中文驗(yàn)證碼功能,結(jié)合實(shí)例形式分析了php中文驗(yàn)證碼功能的實(shí)現(xiàn)步驟與操作方法,包括圖形創(chuàng)建、編碼操作、session操作等相關(guān)技巧,需要的朋友可以參考下2017-01-01
PHP高級對象構(gòu)建 多個構(gòu)造函數(shù)的使用
構(gòu)建對象是PHP面向?qū)ο缶幊淘O(shè)計(jì)中的一個重要主題。在最簡單的情況下,普通構(gòu)造函數(shù)就夠用了,但如果要開展更為復(fù)雜的設(shè)計(jì),那么構(gòu)造函數(shù)可能會變的難以管理2012-02-02
PHP商品秒殺問題解決方案實(shí)例詳解【mysql與redis】
這篇文章主要介紹了PHP商品秒殺問題解決方案,結(jié)合實(shí)例形式詳細(xì)分析了php結(jié)合mysql與redis實(shí)現(xiàn)商品秒殺功能的相關(guān)操作技巧及注意事項(xiàng),需要的朋友可以參考下2019-07-07
php實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的方法
這篇文章主要介紹了php實(shí)現(xiàn)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的方法,通過獲取服務(wù)器端網(wǎng)絡(luò)參數(shù)及文本文件讀寫實(shí)現(xiàn)統(tǒng)計(jì)在線人數(shù)的功能,非常簡單實(shí)用,需要的朋友可以參考下2015-05-05

