Home  >  Article  >  php教程  >  简单的PHP数据后台实现用户登录

简单的PHP数据后台实现用户登录

WBOY
WBOYOriginal
2016-06-06 19:44:461309browse

最近在写iOS一个小项目,用到了 登录 ,所以用SAE搭了个简易的纯 数据 后台 。PHP语句入门很 简单 ,但是结合SQL就有点难度了。(代码都是SAE平台运行的,估计和PHP有些方法不一样,像SaeMysql就是SAE自己的一些函数) 首先说一下用到的PHP基本语法。 最基本的

  最近在写iOS一个小项目,用到了登录,所以用SAE搭了个简易的纯数据后台。PHP语句入门很简单,但是结合SQL就有点难度了。(代码都是SAE平台运行的,估计和PHP有些方法不一样,像SaeMysql就是SAE自己的一些函数)

  首先说一下用到的PHP基本语法。

  最基本的就是从客户端的请求里,把参数提取出来,在PHP里面有三种方法,GET和POST,对应客户端的请求方式。还有一种全能的,如下

 

<span>1</span> <span>$username</span> = <span>$_REQUEST</span>['username'];    <span>//</span><span>单引号里是参数名</span>
<span>2</span> <span>$password</span> = <span>$_REQUEST</span>['password'];  

  不管客户端是GET请求还是POST,用REQUEST都能提取到参数。_GET和_POST看需求

  以上是提取数据,然后呢是注册登录。现在先写一下登录吧,注册的时候要说一下数据库怎么创建,再细说。

  登录呢,思路是在数据库里查找有没有和提取到的username参数相匹配的元素,如果有,继续匹配password,否则返回登录失败。首先看一下怎么查询,用到SQL的查询语句,怎么执行并返回查询到的数据呢,用SAE里面的getData方法。

<span>1</span> <span>//</span><span>查询语句</span>
<span>2</span> <span>$sql</span> = "select * from 表名 where username='username' "<span>;
</span><span>3</span> <span>//</span><span>创建<strong>数据</strong>库对象,以执行上面的SQL语句</span>
<span>4</span> <span>$mysql</span> = <span>new</span><span> SaeMysql();
</span><span>5</span> <span>//</span><span>执行完毕后会返回一个数组</span>
<span>6</span> <span>$arr</span> = <span>$mysql</span> -> getData(sql);

  查询完毕之后呢,要进行判断了。首先看看数组里有没有元素,来判断数据表里有没有对应的用户

<span>1</span> <span>if</span> (<span>count</span>(arr) == 0<span>)
</span><span>2</span> <span>{
</span><span>3</span>         <span>//</span><span>用JSON返回1代表账户不存在</span>
<span>4</span>         <span>$dic</span> = <span>array</span>('success' => '1'<span>);
</span><span>5</span>         <span>echo</span> json_encode(<span>$dic</span><span>);
</span><span>6</span> }    

  如果用户存在,就需要进一步判断密码是否正确了。

<span> 1</span> <span>else</span>
<span> 2</span> <span>{
</span><span> 3</span>         <span>//</span><span>从数组里取出<strong>用户</strong>字典</span>
<span> 4</span>         <span>$userDic</span> = <span>$arr</span>[0<span>];
</span><span> 5</span>         <span>$sqlPassword</span> = <span>$userDic</span>['password'<span>];
</span><span> 6</span>         <span>if</span>(<span>$sqlPassword</span> == <span>$password</span><span>)
</span><span> 7</span> <span>        {
</span><span> 8</span>                 <span>$dic</span> = <span>array</span>('success' => '0'<span>);
</span><span> 9</span>                 <span>echo</span> json_encode(<span>$dic</span><span>);
</span><span>10</span> <span>        }
</span><span>11</span>         <span>else</span>
<span>12</span> <span>        {
</span><span>13</span>                 <span>$dic</span> = <span>array</span>('success' => '2'<span>);
</span><span>14</span>                 <span>echo</span> json_encode(<span>$dic</span><span>);
</span><span>15</span> <span>        }
</span><span>16</span> }    

  这样就用JSON返回了登录结果。别忘了要关闭数据库。

<span>1</span> <span>$mysql</span> ->closeDb();

  登录就到这里。注册要麻烦一些,要创建数据库,配置一些属性。另开一篇吧

  

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn