PHP Login Registration API Interface Code

Source: Internet
Author: User
Tags http post urlencode

/** * At a glance (www.yl1001.com) * PHP Development API Interface Server*/require' conn.php ';//files connected to the databaseHeader(' Content-type:text/html;charset=utf-8 '); $action=$_get[' Action ']; Switch($action) {      //Registered Members     Case"Adduserinfo"; $username= Lib_replace_end_tag (Trim($_get[' username ']);//lib_replace_end_tag () preventing SQL injection        $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.html Target=_blankclass=infotextkey>mysql_query()($sql,$conn); $count=mysql_num_rows($query); if($count> 0) {             Exit(Json_encode (1));//returns 1 indicates registration failed}Else {              $addsql= "INSERT INTO ' member ' (Username,password,email) VALUES ('$username‘,‘$password‘,‘$email‘)"; mysql_query($addsql); Exit(Json_encode (0));//returns 0 indicates successful registration        }          Break; //Querying 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("Bad password"); 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 is incorrect*/          Break; default:Exit(Json_encode (Error)); } }
PHP Development API Interface Instance code share a case of PHP implementation API interface code, to achieve user registration, login, query user information functions,


code example: <? php php API Client Part code example:
 <?php
/**
* PHP API Interface Client
* edit:www.jbxue.com
*/
Header (' Content-type:text/html;charset=utf-8 '); Avoid garbled output

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 () ($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)));
}

Reprinted from: http://www.cnblogs.com/mssql8/p/4034842.html

PHP Login Registration API Interface Code

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.