Heim >Backend-Entwicklung >PHP-Tutorial >简单的PHP数据后台实现用户登录,php后台用户登录_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>创建数据库对象,以执行上面的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>从数组里取出用户字典</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();
登录就到这里。注册要麻烦一些,要创建数据库,配置一些属性。另开一篇吧
找个免费的php源代码网站下一个吧.
恩 改了 你试试看
这个我不可能写得直接能连接到你所要求的数据库的,因为不知道你数据库信息与表的设计,所以你要自己更换代码里的hostname,db_user,password,db以及表名,字段名,更换了应当就可以直接使用的,用户名密码正确与错误我做了修改
session_start();
$host = "localhost"; //服务器名称
$db_user = "root"; //用户名
$db_password = "74862856"; //密码
$db = "TEST"; //所要连接的数据库
$link_id = @ mysql_connect($host,$db_user,$db_password) or die("连接数据库失败".mysql_error());
$db_selected = mysql_select_db($db,$link_id);
if(!$db_selected){
die("未找到指定的数据库".mysql_error());
}
if(isset($_COOKIE['user'])){
$sql = 'select * from name where user="'.$_COOKIE['user'].'"';
$result = @ mysql_query($sql,$link_id) or die("SQL语句出错");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
if(isset($row)){ //如果数据库中存在该用户
Header("Location:index.php"); //合法COOKIE直接跳转到指定界面
}else{
$_COOKIE['user'] = ""; //非法COOKIE清空
Header("Location:login.php"); //重新载入界面
}
}
if(isset($_POST['submitted'])){
$user = $_POST['user'];
$pwd = $_POST['pwd'];
$sql = 'select * from name where user="'.$user.'"';
$result = @ mysql_query($sql,$link_id) or die("SQL语句出错");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$cmp_pwd = $row['password'];
if($cmp_p......余下全文>>