码迷,mamicode.com
首页 > Web开发 > 详细

PHP PDO 简单登陆操作

时间:2015-05-30 16:37:38      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

用PHP做出一个简单的登陆操作,确实很简单,下面就让我给大家简单的介绍一下PDO做出一个登陆界面操作的过程,因为也是初学乍练,不足之处请大家包涵。

首先,首先还要建一个表,在MySQL中建表,核心代码如下:

 1 DROP TABLE IF EXISTS `t_login`;
 2 CREATE TABLE `t_login` (
 3   `userid` int(4) NOT NULL DEFAULT 0,
 4   `username` varchar(20) DEFAULT NULL,
 5   `userpass` varchar(20) DEFAULT NULL,
 6   `userphone` varchar(25) DEFAULT NULL,
 7   `useraddress` varchar(50) DEFAULT NULL,
 8   PRIMARY KEY (`userid`)
 9 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
10 
11 -- ----------------------------
12 -- Records of t_login
13 -- ----------------------------
14 INSERT INTO `t_login` VALUES (0, 11, 1111111, 1111, 111);
15 INSERT INTO `t_login` VALUES (1, admin, admin, 1212, 111111);

  下面就来具体讲一下操作流程,建一个项目,命名为P_U,在项目中新建两个文件,分别命名为View和Model,新建文件是为了好分层管理,有一个好的习惯是很重要的,所以在初学的时候,有分层的意识是很重要的。View是视图层,这个Model是数据处理层和业务逻辑层吧。

  第一,先做出两个界面来,login.php和res.php,下面依次是login和res的代码。

 1  <form action="../Model/LoginModel.php" method="post">
 2             <div align="center">
 3                 <table>
 4                     <tr>
 5                         <td>姓名:</td>
 6                         <td>
 7                             <input type="text" name="username">
 8                         </td>
 9                     </tr>
10                      <tr>
11                         <td>密码:</td>
12                         <td>
13                             <input type="password" name="userpass">
14                         </td>
15                     </tr>
16                      <tr>
17                         <td></td>
18                         <td>
19                             <input type="submit" value="登陆">
20                             <input type="reset" value="重置">
21                         </td>
22                     </tr>
23                 </table>
24                 <a href="res.php">没有账号,请注册</a>
25             </div>
26         </form>

 

 

 <form action="../Model/ResModel.php" method="post">
                <table>
                    <tr>
                        <td>用户名:</td>
                        <td>
                            <input type="text" name="username">
                        </td>
                    </tr>
                    <tr>
                        <td>登陆密码</td>
                        <td>
                            <input type="text" name="userpass">
                        </td>
                    </tr>
                    <tr>
                        <td>重复密码:</td>
                        <td>
                            <input type="text" name="userpassagin">
                        </td>
                    </tr>
                    <tr>
                        <td>电话:</td>
                        <td>
                            <input type="text" name="userphone">
                        </td>
                    </tr>
                    <tr>
                        <td>住址:</td>
                        <td>
                            <input type="text" name="useraddress">
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <input type="submit" value="提交">
                        </td>
                    </tr>
                </table>
            </form>

 

在这个里面,我就不写JS代码了,因为就是一个表单验证,方法很多,百度的资料也特别的多。

  二,在Model里面下工夫了,建立一个LoginModel.php和ResModel.php,下面是它们的代码:

LoginModel.php代码如下:

 1 <?php
 2     @$username = $_POST[username];
 3     @$userpass = $_POST[userpass];
 4     
 5     $dbms = "mysql";//选择数据库类型,MySQL
 6     $host = "127.0.0.1"; //选择服务器
 7     $userName = "";//用户名
 8     $psw = "";
 9     $dbName = "dbtext";//数据库名称
10     $dsn = "$dbms:host=$host;dbname=$dbName";
11     
12     try {
13         $pdo = new PDO($dsn, $userName, $psw);
14         $query = "select * from t_login where username=:username and userpass = :userpass";
15         $request = $pdo->prepare($query);
16         $request->bindParam(‘:username‘, $username);
17         $request->bindParam(‘:userpass‘, $userpass);
18         $request->execute();
19         $res = $request->fetchAll(PDO::FETCH_ASSOC);        
20         if (!empty($res)){
21             header(‘Location: http://localhost/P_U/View/main.php‘);
22         }else{
23             header(‘Location: http://localhost/P_U/View/login.php‘);
24         }
25     } catch (Exception $e) {
26         die("Error!!".$e->getMessage());
27     }
28     
29 ?>

 

ResModel.php代码如下:

 1 <?php
 2     @$username = $_POST[username];
 3     @$userpass = $_POST[userpass];
 4     @$userphone = $_POST[userphone];
 5     @$useraddress = $_POST[useraddress];
 6     
 7     $dbms = "mysql";//选择数据库类型,MySQL
 8     $host = "127.0.0.1"; //选择服务器
 9     $userName = "ecstore";//用户名
10     $psw = "ecstore2014!@#";
11     $dbName = "dbtext";//数据库名称
12     $dsn = "$dbms:host=$host;dbname=$dbName";
13     
14     try {
15         $pdo = new PDO($dsn, $userName, $psw);
16         $query = "insert into t_login(username,userpass,userphone,useraddress) VALUES (:username,:userpass,:userphone,:useraddress)";
17         $request = $pdo->prepare($query);
18         
19         $request->bindParam(‘:username‘, $username);
20         $request->bindParam(‘:userpass‘, $userpass);
21         $request->bindParam(‘:userphone‘, $userphone);
22         $request->bindParam(‘:useraddress‘, $useraddress);
23         
24         $res = $request->execute();
25         if(!empty($res)){
26             echo "注册成功!!";
27             echo "<a href=‘http://localhost/P_U/View/login.php‘>返回登陆界面</a>";
28         }
29         
30     } catch (Exception $e) {
31         die("注册失败!!!".$e->getMessage());
32     }
33     
34 ?>

 

好了,随便写一个main.php界面吧,登陆成功后就自动跳到main.php界面上。

百度云资料,源码下载连接:http://pan.baidu.com/s/1dDdagEl

php就是这么简单,好好的学,总会有收获,希望能帮到你。

PHP PDO 简单登陆操作

标签:

原文地址:http://www.cnblogs.com/invban/p/4540524.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!