This article mainly introduces the PHP implementation of the registration, login and query user Data API interface, combined with specific examples of PHP server-side user registration, login, query and Curl Landing query function implementation skills, the need for friends can refer to the next
This article describes the PHP implementation of the registration, login and query user Data function API interface. Share to everyone for your reference, as follows:
Service side
<?phprequire ' conn.php '; header (' Content-type:text/html;charset=utf-8 '); $action = $_get[' action '];switch ($ Action) {//Registered member case "Adduserinfo"; $username = Lib_replace_end_tag (Trim ($_get[' username ')); $password 2 = Lib_replace_end_tag (Trim ($_get[' userpassword ')); $password = MD5 ("$password 2". ALL_PS); $email = Lib_replace_end_tag (Trim ($_get[' email ')); if ($username = = "| | $password 2 = =" ' | | $password = = ') {$res = UrlEncode ("wrong parameter"); Exit (Json_encode ($res)); Free information} $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));//return 1 means registration failed} else {$addsql = "insert INTO ' member ' (Username,pas Sword,email) VALUES (' $username ', ' $password ', ' $email '); mysql_query ($addsql); Exit (Json_encode (0)); Returns 0 for registration success} break; Query user Information 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 Member Login Case "Userlogin"; $username = Lib_replace_end_tag ($_get[' username '); $password 2 = Lib_replace_end_tag (Trim ($_get[' userpassword ')); $password = MD5 ("$password 2". 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 ("Login Successful "); Exit (Json_encode ($res)); } else {$res =UrlEncode ("Password error"); Exit (Json_encode ($res)); }} else {$res = UrlEncode ("User name does not exist"); Exit (Json_encode ($res)); }} else {$res = UrlEncode ("User name password error"); Exit (Json_encode ($res)); }/* * 0: Indicates successful login, 1: Password error, 2: User name does not exist, 3: User name password error */break; Default:exit (Json_encode (Error));}? >
client Example:
<?phpheader (' Content-type:text/html;charset=utf-8 '); Avoid output garbled 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! = | | | 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 ($postRe Sult); 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);p Rint_r (UrlDecode (Json_encode ($res)));? >
Note: the function in the code lib_replace_end_tag
is a custom string filter function, for reference: An analysis of PHP filtering HTML strings to prevent SQL injection method