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

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

 DROP TABLE IF EXISTS `t_login`;
CREATE TABLE `t_login` (
`userid` int() NOT NULL DEFAULT '',
`username` varchar() DEFAULT NULL,
`userpass` varchar() DEFAULT NULL,
`userphone` varchar() DEFAULT NULL,
`useraddress` varchar() DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of t_login
-- ----------------------------
INSERT INTO `t_login` VALUES ('', '', '', '', '');
INSERT INTO `t_login` VALUES ('', 'admin', 'admin', '', '');

  下面就来具体讲一下操作流程,建一个项目,命名为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就是这么简单,好好的学,总会有收获,希望能帮到你。

最新文章

  1. arcgis api for flex之专题图制作(饼状图,柱状图等)
  2. python之路三
  3. 我想有个梦想(I want have a dream)
  4. (转载)C语言 数组与指针的区别
  5. wampserver 2.4 配置虚拟主机
  6. MIG IP控制DDR3读写测试
  7. C语言循环的实现
  8. JAVA 连等赋值问题
  9. ubuntu16.04 backup and restore
  10. H5外包团队 2019案例更新
  11. 找到排序矩阵中从小到大第K个数字
  12. Redis入门篇(安装与启动)
  13. Loj 【CQOI 2006】简单题,mmp
  14. springboot-async
  15. 20165317Java实验五 网络编程与安全
  16. apache ab 压力测试
  17. tips:Java中的switch的选择因子
  18. docker-ce-17.09 容器创建,运行,进入,删除,导入/导出
  19. ajax请求也可以用form表单向后台提交数据!!!!
  20. 20155338《网络对抗》Exp6 信息搜集与漏洞扫描

热门文章

  1. [转]是String,StringBuffer还是StringBuilder?
  2. javascript 基础学习整理
  3. wp8模拟器中使用电脑键盘和模拟器的版本解释
  4. LeetCode--判断二叉树是否对称
  5. floyd详解
  6. Codeforces Round #326 (Div. 1) - C. Duff in the Army 树上倍增算法
  7. C - Catch That Cow
  8. Tomcat Server Locations
  9. Lucene实例教程
  10. __block的作用