想要自己一个人完成app,那么后台接口也必须自己动动手。不用担心,其实很简单的,给自己信心!
下面就以登录注册为例,做一个api接口

首先在mac上搭建PHP环境,下载 MAMP Pro for Mac 3.4 破解版:

http://www.ifunmac.com/2015/08/mamp-pro-3-4/
即可一键安装Apache/PHP/MySQL开发环境。简单吧。

有了环境就可以写代码了:

首先写一个Config.php (配置数据库)

1 <?php
2
3 //定义数据库连接所需的变量
4 define("DB_HOST", "localhost");
5 define("DB_USER", "root");
6 define("DB_PASSWORD", "master12!");
7 define("DB_DATABASE", "loginAPI");
8
9 ?>

写一个DB_Connect.php(用于连接数据库)

 1 <?php
2
3 class DB_Connect
4 {
5 public $con;
6
8 function __construct()
9 {
10
11 }
12
14 function __destruct()
15 {
16 // $this->close();
17 }
18
19 //连接数据库
20 public function connect()
21 {
22 require_once 'include/Config.php';
23 //连接mysql
24 $this->con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysqli_error($this->con));
25 if (mysqli_connect_errno()) {
26 die("Database connection failed");
27 }
28
29 // 返回 database handler
30 return $this->con;
31 }
32
33 //关闭数据连接
34 public function close()
35 {
36 mysqli_close($this->con);
37 }
38
39 }
40
41 ?>

再来一个:DB_Functions.php (用来封装 执行sql后 返回数据的方法)

  1 <?php
2
3 class DB_Functions {
4
5 private $db;
6
7 // constructor
8 function __construct() {
9 require_once 'DB_Connect.php';
10 // connecting to database
11 $this->db = new DB_Connect();
12 $this->db->connect();
13 }
14
15 // destructor
16 function __destruct() {
17
18 }
19
20 /**
21 * 添加用户信息
22 */
23 public function storeUser($name, $email, $password) {
24 $uuid = uniqid('', true);
25 $hash = $this->hashSSHA($password);
26 $encrypted_password = $hash["encrypted"]; // 加密后的密文
27 $salt = $hash["salt"]; // salt
28 $result = mysqli_query($this->db->con,"INSERT INTO users(unique_id, name, email, encrypted_password, salt, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW())");
29 // 检查结果
30 if ($result) {
31 // 获取用户信息
32 $uid = mysqli_insert_id($this->db->con); // 获取最新的id
33 $result = mysqli_query($this->db->con,"SELECT * FROM users WHERE uid = $uid");
34 //返回刚插入的用户信息
35 return mysqli_fetch_array($result);
36 } else {
37 return false;
38 }
39 }
40
41 /**
42 * 通过email和password获取用户信息
43 */
44 public function getUserByEmailAndPassword($email, $password) {
45 $result = mysqli_query($this->db->con,"SELECT * FROM users WHERE email = '$email'") or die(mysqli_connect_errno());
46 // check for result
47 $no_of_rows = mysqli_num_rows($result);
48 if ($no_of_rows > 0) {
49 $result = mysqli_fetch_array($result);
50 $salt = $result['salt'];
51 $encrypted_password = $result['encrypted_password'];
52 $hash = $this->checkhashSSHA($salt, $password);
53 // check for password
54 if ($encrypted_password == $hash) {
55 return $result;
56 }
57 } else {
58 return false;
59 }
60 }
61
62 /**
63 * 通过email判断用户是否存在
64 */
65 public function isUserExisted($email) {
66 $result = mysqli_query($this->db->con,"SELECT email from users WHERE email = '$email'");
67 $no_of_rows = mysqli_num_rows($result);
68 if ($no_of_rows > 0) {
69 // 用户存在
70 return true;
71 } else {
72 //用户不存在
73 return false;
74 }
75 }
76
77 /**
78 * 加密
79 * @param password
80 * returns salt and encrypted password
81 */
82 public function hashSSHA($password) {
83
84 $salt = sha1(rand());
85 $salt = substr($salt, 0, 10);
86 $encrypted = base64_encode(sha1($password . $salt, true) . $salt);
87 $hash = array("salt" => $salt, "encrypted" => $encrypted);
88 return $hash;
89 }
90
91 /**
92 * 解密
93 * @param salt, password
94 * returns hash string
95 */
96 public function checkhashSSHA($salt, $password) {
97
98 $hash = base64_encode(sha1($password . $salt, true) . $salt);
99
100 return $hash;
101 }
102
103 }
104
105 ?>

在Index.php调用并输出返回值(这个文件地址就是接口的访问地址)

 1 <?php
2
3 if (isset($_POST['tag']) && $_POST['tag'] != '') {
4 // tag是接口请求时post的值(方法名称),用来区别调用方法
5 $tag = $_POST['tag'];
6
7 //引用DB_Functions.php
8 require_once 'include/DB_Functions.php';
9 $db = new DB_Functions();
10
11 // 定义输入数组
12 $response = array("tag" => $tag, "error" => FALSE);
13
14 // 判断tag值
15 if ($tag == 'login') {
16 //获取login方法的post参数
17 $email = $_POST['email'];
18 $password = $_POST['password'];
19
20 // 通过email 和password获取用户信息
21 $user = $db->getUserByEmailAndPassword($email, $password);
22 if ($user != false) {
23 //找到用户信息
24 $response["error"] = FALSE;
25 $response["uid"] = $user["unique_id"];
26 $response["user"]["name"] = $user["name"];
27 $response["user"]["email"] = $user["email"];
28 $response["user"]["created_at"] = $user["created_at"];
29 $response["user"]["updated_at"] = $user["updated_at"];
30 echo json_encode($response);
31 } else {
32 //没有找到用户信息
33 //输出错误信息
34 $response["error"] = TRUE;
35 $response["error_msg"] = "帐号或密码不正确!";
36 echo json_encode($response);
37 }
38 } else if ($tag == 'register') {
39 //注册帐号
40 $name = $_POST['name'];
41 $email = $_POST['email'];
42 $password = $_POST['password'];
43
44 // 判断用户是否存在
45 if ($db->isUserExisted($email)) {
46 // 如果用户存在就返错误提示
47 $response["error"] = TRUE;
48 $response["error_msg"] = "用户已存在";
49 echo json_encode($response);
50 } else {
51 // 新增用户
52 $user = $db->storeUser($name, $email, $password);
53 if ($user) {
54 //新增成功返回用户信息
55 $response["error"] = FALSE;
56 $response["uid"] = $user["unique_id"];
57 $response["user"]["name"] = $user["name"];
58 $response["user"]["email"] = $user["email"];
59 $response["user"]["created_at"] = $user["created_at"];
60 $response["user"]["updated_at"] = $user["updated_at"];
61 echo json_encode($response);
62 } else {
63 // 新增失败,返回错误信息
64 $response["error"] = TRUE;
65 $response["error_msg"] = "服务器繁忙,操作失败";
66 echo json_encode($response);
67 }
68 }
69 } else {
70 // tag值无效时
71 $response["error"] = TRUE;
72 $response["error_msg"] = "未找到您要的方法";
73 echo json_encode($response);
74 }
75 } else {
76 $response["error"] = TRUE;
77 $response["error_msg"] = "您的参数不正确!";
78 echo json_encode($response);
79 }
80 ?>

转自:http://www.cnblogs.com/kissdodog/p/4159176.html

http://www.cnblogs.com/kissdodog/p/4159176.html的更多相关文章

  1. javascript Windouw 转自 http://www.cnblogs.com/kissdodog/archive/2013/01/01/2841464.html

    javascript之window对象 window :window对象是BOM中所有对象的核心,除了是BOM中所有对象的父对象外,还包含一些窗口控制函数. 1.全局的window对象 JavaScr ...

  2. Mysql----索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  3. mysql六:索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  4. 初谈SQL Server逻辑读、物理读、预读

    前言 本文涉及的内容均不是原创,是记录自己在学习IO.执行计划的过程中学习其他大牛的博客和心得并记录下来,之所以想写下来是为了记录自己在追溯的过程遇到的几个问题,并把这些问题弄清楚. 本章最后已贴出原 ...

  5. ajaxFileUpload插件

    关键词: $.ajaxFileUpLoad(); data status dataType 参考资料: http://www.cnblogs.com/kissdodog/archive/2012/12 ...

  6. 【转】MySql中的函数

    原文:http://www.cnblogs.com/kissdodog/p/4168721.html MySQL数据库提供了很多函数包括: 数学函数: 字符串函数: 日期和时间函数: 条件判断函数: ...

  7. Comet技术

    1.Comet是什么? 维基百科: Comet是一种用于web的推送技术,能使服务器实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流. 说白了就是web ...

  8. Xml序列化去掉命名空间,去掉申明

    #region 序列化        /// <summary>        /// 序列化        /// </summary>        /// <par ...

  9. .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别

    WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...

随机推荐

  1. SharePoint 2013 日期和时间字段格式设置

    前言 最近碰到一个需求,用户希望修改日期和时间字段的格式,因为自己的环境是英文的,默认的时间格式是[月/日/年]这样的格式,我也是碰到这个问题才知道,这是美式的时间格式,然而用户希望变成英式的时间格式 ...

  2. JVM之上的语言小集

    1 JVM上的编程语言https://en.wikipedia.org/wiki/List_of_JVM_languages主要的有:Clojure, a functional Lisp dialec ...

  3. 基于Python+Django的Kubernetes集群管理平台

    ➠更多技术干货请戳:听云博客 时至今日,接触kubernetes也有一段时间了,而我们的大部分业务也已经稳定地运行在不同规模的kubernetes集群上,不得不说,无论是从应用部署.迭代,还是从资源调 ...

  4. iOS开发--面试

    今天一大清早去面试, 公司距离我家还挺近的, 花了一个小时走着去, 也顺路印下简历, 理理思路, 到了公司面试官什么的都不错, 还给我讲了很多知识, 收货也是满满的, 总结下今天都遇到了哪些问题, 调 ...

  5. Laravel大型项目系列教程(二)之用户管理

    Laravel大型项目系列教程(二) 一.前言 本节教程将大概实现用户的注册.修改个人信息.管理用户功能. 二.Let's go 1.创建用户注册视图 $ php artisan generate:v ...

  6. django之一些feature

    前端之django一些feature 本节内容 cookie session 跨站请求保护 分页 序列化 model模块 CBV和FBV 模板渲染对象 1. cookie cookie 是一种发送到客 ...

  7. react自学笔记总结不间断更新

    React React 是由Facfbook维护的一套框架,并且引用到instagram React只是我们熟悉MVC框中的V层,只是视图层面的一个框架,只有俩个半api(createClass,cr ...

  8. Centos6下安装高版本Git

    yum remove git .tar.gz /usr/src/ cd /usr/src/ cd git-/ make configure whereis autoconf yum install a ...

  9. Nuget 命令 NuGet 管理项目库

    因为可视化库程序包管理器的局限性,有很多需要的功能在界面中无法完成. 以下技巧均需要在"程序包管理器控制台"中使用命令来完成. 一.改变项目目标框架后,更新程序包 当改变项目的目标 ...

  10. ttf,eot,woff,svg,字体格式介绍及使用方法

    而由于网页中使用的字体类型,也是各浏览器对字体类型有不同的支持规格. 字体格式类型主要有几个大分类:TrueType.Embedded Open Type .OpenType.WOFF .SVG. T ...