Home  >  Article  >  Backend Development  >  Simple PHP data background to implement user login, php background user login_PHP tutorial

Simple PHP data background to implement user login, php background user login_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:191368browse

Simple PHP data background to realize user login, PHP background user login

Recently I was writing a small project for iOS, which used login, so I used SAE to build a simple pure data Backstage. Getting started with PHP statements is easy, but combining it with SQL is a bit more difficult. (The code is all run on the SAE platform. It is estimated that some methods are different from PHP. For example, SaeMysql is some of SAE's own functions)

First let’s talk about the basic syntax of PHP used.

The most basic thing is to extract the parameters from the client's request. There are three methods in PHP, GET and POST, which correspond to the client's request method. There is also an almighty one, as follows

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

Regardless of whether the client is requesting GET or POST, parameters can be extracted using REQUEST. _GET and _POST depend on the requirements

The above is to extract data, and then register and log in. Now let’s write about the login first. When registering, we will talk about how to create the database and then go into details.

As for logging in, the idea is to find in the database whether there is an element that matches the extracted username parameter. If so, continue to match the password, otherwise it will return login failure. First, let’s take a look at how to query, use SQL query statements, how to execute and return the queried data, and use the getData method in SAE.

<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);

After the query is completed, it is time to make a judgment. First, check whether there are elements in the array to determine whether there is a corresponding user in the data table.

<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> }    

If the user exists, you need to further determine whether the password is correct.

<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> }    

In this way, the login result is returned in JSON. Don't forget to close the database.

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

Log in here. Registration is a bit more troublesome, as you need to create a database and configure some properties. Let’s write another article

 

How to implement simple php background data entry

Look for a free PHP source code website.

Make a simple login page using PHP language

Yes, please give it a try
I can’t write this in a way that can directly connect to the database you requested, because I don’t know the design of your database information and tables, so you have to change the hostname in the code yourself. db_user, password, db, table name, field name, should be able to be used directly after changing them. I have modified the correct and incorrect username and password

session_start();
$host = "localhost"; //Server name
$db_user = "root"; //User name
$db_password = "74862856"; //Password
$db = "TEST"; / /The database to be connected
$link_id = @ mysql_connect($host,$db_user,$db_password) or die("Failed to connect to the database".mysql_error());
$db_selected = mysql_select_db($db,$link_id );
if(!$db_selected){
die("The specified database was not found".mysql_error());
}

if(isset($_COOKIE['user' ])){

$sql = 'select * from name where user="'.$_COOKIE['user'].'"';
$result = @ mysql_query($sql,$link_id ) or die("SQL statement error");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
if(isset($row)){ //If the user exists in the database
Header( "Location:index.php"); //Legal COOKIE jumps directly to the specified interface
}else{
$_COOKIE['user'] = ""; //Clear illegal COOKIE
Header(" Location:login.php"); //Reload interface
}

}

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 statement error");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$cmp_pwd = $row['password '];
if($cmp_p...the rest of the text>>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/905332.htmlTechArticleSimple PHP data background to implement user login, php background user login. Recently, I was writing a small project for iOS and used login. , so I used SAE to build a simple pure data backend. Getting started with PHP statements is very easy...
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