PHP实现的注册,登录及查询用户资料功能API接口示例
作者:程序加载中 发布时间:2024-04-30 08:50:18
标签:PHP,注册,登录,查询
本文实例讲述了PHP实现的注册,登录及查询用户资料功能API接口。分享给大家供大家参考,具体如下:
服务端
<?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("参数有误");
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
函数为自定义字符串过滤函数,具体可参考:浅析php过滤html字符串,防止SQL注入的方法
希望本文所述对大家PHP程序设计有所帮助。
0
投稿
猜你喜欢
- 这篇文章主要介绍了基于python读取.mat文件并取出信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需
- 背景有一个BUG,就是在使用element弹窗表格的字体异常的模糊。如下图:这个问题其实已经存在很久了。客户屡有反馈,但是不多。我们基本自测
- 1、为图片加入水印功能 Dim Jpeg Set Jpeg = Server.Create
- 在 asp 应用中,经常用到 Session 对象来保存用户临时私有数据,而 asp 的 Session 对象是依赖于浏览器的 Cookie
- 由于服务器的数据库硬盘空间满了,由于大量写入数据失败导致了出现“Duplicate entry '' for key
- 数据预处理在解决深度学习问题的过程中,往往需要花费大量的时间和精力。 数据处理的质量对训练神经网络来说十分重要,良好的数据处理不仅会加速模型
- jQuery 简介jQuery 库可以通过一行简单的标记被添加到网页中。您需要具备的基础知识在您开始学习 jQuery 之前,您应该对以下知
- Python信息抽取之乱码解决办法就事论事,直说自己遇到的情况,和我不一样的路过吧,一样的就看看吧信息抓取,用python,beautifu
- 1、背景由于办公需要“每天定时推送某消息用来提醒群里面所有人”,于是决定用企业微信自带的机器人来实现此功能。具体方法我来一一讲述。2、企业微
- 新建项目如下图,比如sigma目录是我要上传的项目,在six-sigma目录下新建三个文件,分别是LICENSE也就是开源协议,README
- 目录前言一、样式穿透1. 什么是样式穿透?2. 如何使用?二、混入1. 什么是混入?2. 如
- mysql_query("set autocommit=0"); $list_one = $db->fetch_f
- 一、简单使用入门小案例import logginglogging.basicConfig(level=logging.DEBUG, &nbs
- 浏览器对于CSS的支持问题落后于CSS的发展,以占有市场绝对份额的Internet Explorer来说,直到其前不久发布的第8个版本才刚刚
- 首先,先要确定你成功安装了MySQL。1、万能启动法win+R打开运行窗口,输入 services.msc在里面找到mysql的服务名,比如
- 我们工作中经常需要将数据转化成柱状图,饼图等,以方便直观的分析数据, 这里给大家介绍一个ASP中制作饼图、柱状图的组件:csDra
- 其实我们平时在深度学习中所说的卷积操作,在 opencv 中也可以进行,或者说是类似操作。那么它是什么操作呢?它就是图像的模糊(滤波)处理。
- 前言体能状态先于精神状态,习惯先于决心,聚焦先于喜好。SHA算法简介1.1 概述SHA (Secure Hash Algorithm,译作安
- 前言这是个在写计算机网络课设的时候碰到的问题,卡了我一天,所以总结一下。其实在之前就有用requests写过python爬虫,但是计算机网络
- 本文实例讲述了Python进阶之使用selenium爬取淘宝商品信息功能。分享给大家供大家参考,具体如下:# encoding=utf-8_