PHP PDO 简单登陆操作
2015-05-30 16:01
736 查看
用PHP做出一个简单的登陆操作,确实很简单,下面就让我给大家简单的介绍一下PDO做出一个登陆界面操作的过程,因为也是初学乍练,不足之处请大家包涵。
首先,首先还要建一个表,在MySQL中建表,核心代码如下:
下面就来具体讲一下操作流程,建一个项目,命名为P_U,在项目中新建两个文件,分别命名为View和Model,新建文件是为了好分层管理,有一个好的习惯是很重要的,所以在初学的时候,有分层的意识是很重要的。View是视图层,这个Model是数据处理层和业务逻辑层吧。
第一,先做出两个界面来,login.php和res.php,下面依次是login和res的代码。
在这个里面,我就不写JS代码了,因为就是一个表单验证,方法很多,百度的资料也特别的多。
二,在Model里面下工夫了,建立一个LoginModel.php和ResModel.php,下面是它们的代码:
LoginModel.php代码如下:
ResModel.php代码如下:
好了,随便写一个main.php界面吧,登陆成功后就自动跳到main.php界面上。
百度云资料,源码下载连接:http://pan.baidu.com/s/1dDdagEl
php就是这么简单,好好的学,总会有收获,希望能帮到你。
首先,首先还要建一个表,在MySQL中建表,核心代码如下:
DROP TABLE IF EXISTS `t_login`; CREATE TABLE `t_login` ( `userid` int(4) NOT NULL DEFAULT '0', `username` varchar(20) DEFAULT NULL, `userpass` varchar(20) DEFAULT NULL, `userphone` varchar(25) DEFAULT NULL, `useraddress` varchar(50) DEFAULT NULL, PRIMARY KEY (`userid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of t_login -- ---------------------------- INSERT INTO `t_login` VALUES ('0', '11', '1111111', '1111', '111'); INSERT INTO `t_login` VALUES ('1', 'admin', 'admin', '1212', '111111');
下面就来具体讲一下操作流程,建一个项目,命名为P_U,在项目中新建两个文件,分别命名为View和Model,新建文件是为了好分层管理,有一个好的习惯是很重要的,所以在初学的时候,有分层的意识是很重要的。View是视图层,这个Model是数据处理层和业务逻辑层吧。
第一,先做出两个界面来,login.php和res.php,下面依次是login和res的代码。
<form action="../Model/LoginModel.php" method="post"> <div align="center"> <table> <tr> <td>姓名:</td> <td> <input type="text" name="username"> </td> </tr> <tr> <td>密码:</td> <td> <input type="password" name="userpass"> </td> </tr> <tr> <td></td> <td> <input type="submit" value="登陆"> <input type="reset" value="重置"> </td> </tr> </table> <a href="res.php">没有账号,请注册</a> </div> </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代码如下:
<?php @$username = $_POST[username]; @$userpass = $_POST[userpass]; $dbms = "mysql";//选择数据库类型,MySQL $host = "127.0.0.1"; //选择服务器 $userName = "";//用户名 $psw = ""; $dbName = "dbtext";//数据库名称 $dsn = "$dbms:host=$host;dbname=$dbName"; try { $pdo = new PDO($dsn, $userName, $psw); $query = "select * from t_login where username=:username and userpass = :userpass"; $request = $pdo->prepare($query); $request->bindParam(':username', $username); $request->bindParam(':userpass', $userpass); $request->execute(); $res = $request->fetchAll(PDO::FETCH_ASSOC); if (!empty($res)){ header('Location: http://localhost/P_U/View/main.php'); }else{ header('Location: http://localhost/P_U/View/login.php'); } } catch (Exception $e) { die("Error!!".$e->getMessage()); } ?>
ResModel.php代码如下:
<?php @$username = $_POST[username]; @$userpass = $_POST[userpass]; @$userphone = $_POST[userphone]; @$useraddress = $_POST[useraddress]; $dbms = "mysql";//选择数据库类型,MySQL $host = "127.0.0.1"; //选择服务器 $userName = "ecstore";//用户名 $psw = "ecstore2014!@#"; $dbName = "dbtext";//数据库名称 $dsn = "$dbms:host=$host;dbname=$dbName"; try { $pdo = new PDO($dsn, $userName, $psw); $query = "insert into t_login(username,userpass,userphone,useraddress) VALUES (:username,:userpass,:userphone,:useraddress)"; $request = $pdo->prepare($query); $request->bindParam(':username', $username); $request->bindParam(':userpass', $userpass); $request->bindParam(':userphone', $userphone); $request->bindParam(':useraddress', $useraddress); $res = $request->execute(); if(!empty($res)){ echo "注册成功!!"; echo "<a href='http://localhost/P_U/View/login.php'>返回登陆界面</a>"; } } catch (Exception $e) { die("注册失败!!!".$e->getMessage()); } ?>
好了,随便写一个main.php界面吧,登陆成功后就自动跳到main.php界面上。
百度云资料,源码下载连接:http://pan.baidu.com/s/1dDdagEl
php就是这么简单,好好的学,总会有收获,希望能帮到你。
相关文章推荐
- PHP 的ORM的理解
- jpush极光推送
- PHP字符串处理
- laravelbook
- PHP中函数
- PHP preg_match正则表达
- ThinkPHP框架完全解析六
- 解决phpmyadmin打不开问题
- 使用PHP自带的filter函数进行数据校验
- PHP连接MySQL的时候报错SQLSTATE[HY000] [2002] No such file or directory
- PHP沉思录(转载)
- PHP 的 BEncode 操作类(Torrent 文件编码)
- PHP 魔术变量
- PHP+IIS的配置
- 优化PHP代码的40条建议
- PHP 超级全局变量
- 解决ftp连接出现 无法从控制 Socket 读取。Socket 错误 = #10054。
- php+mysql 安全
- thinkphp 在数据库更新(save)的时候,如果字段没有被修改,会更新失败 和数据的自动验证,插入的时候可以自动验证,但更新的时候就不能验证了
- 《PHP框架ThinkPHP学习》系列技术文章整理收藏