PHP-implemented registration, login, and query of user information function API example, user information api

Source: Internet
Author: User

PHP-implemented registration, login, and query of user information function API example, user information api

This example describes the API interface for registering, logging on to, and querying user information implemented by PHP. We will share this with you for your reference. The details are as follows:

Server

<? 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']); $ password2 = lib_replace_end_tag (trim ($ _ GET ['userpassword']); $ password = md5 ("$ password2 ". ALL_PS); $ email = lib_replace_end_tag (trim ($ _ GET ['email ']); if ($ username = ''| $ password2 ='' | $ pass Word = '') {$ res = urlencode (" parameter error "); exit (json_encode ($ res )); // available 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 indicating registration Failed} else {$ addsql = "insert into 'member' (username, password, email) values ('$ username',' $ password ', '$ email') "; mysql_query ($ addsql); exit (j Son_encode (0); // return 0 indicates successful registration} 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 logon case "userlogin"; $ usernam E = 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 ("Logon successful"); exit (json_encode ($ res ));} else {$ res = urlencode ("Incorrect password"); exit (json_encode ($ res) ;}} else {$ res = urlencode ("the user name does not exist "); exit (json_encode ($ res) ;}} else {$ res = urlencode ("incorrect user name and password"); exit (json_encode ($ res);}/** 0: indicates that the logon is successful. 1: indicates that the password is incorrect. Error: 2: the user name does not exist; 3: the user name and password are incorrect */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! = 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.ph P "; $ 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);?>

Note:In the codelib_replace_end_tagThe function is a custom string filter function. For details, refer to: analysis of how php filters html strings to prevent SQL injection.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.