您的位置:首页 > 编程语言 > PHP开发

ThinkPHP内容管理系统开发日记(四)-- 配置信息与开发Longin模块以及模板

2016-09-01 11:49 603 查看
配置信息如下/php/user/conf/config.php

<?php
return array(
//'配置项'=>'配置值'
// /* 数据库设置 */
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'php', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => '******', // 密码
'DB_PREFIX' => 'php_', // 数据库表前缀
'DB_CHARSET'=>'utf8', //数据库字符集编码
'DB_PORT'=>'3306',//数据库端口

'SHOW_PAGE_TRACE'=>true, //显示页面trace信息。
);
?>

在Action 文件夹下创建Login模块:
/php/user/lib/action/LoginAction.class.php

命名规则:模块名+Action.class.php

<?php
//定义loginAction类
class LoginAction extends Action{
function index(){
/*
*显示默认模板:默认模板为TPL/控制器名(这里为Login)/index.html
*/
$this->show();
}

function chklogin(){
/*
*登录验证方法,获取表单中提交的内容,实例化模型获取对应数据库的信息。
*/
//开启session
@session_start();

//下面是通过表单中:<input type="hidden" name="dopost" value="login" />来判断是否提交表单
if($_POST['dopost'] == 'login'){
//无验证码验证,如果有则需要先判断验证码
//取提交表单中的密码并计算MD5
$password = md5($_POST['password']);
//获取提交表单中的username
$username = $_POST['username'];
//实例化user 模型
$User = M('user');
//获取数据库中与提交的$username相同的行
$querydata = $User->getByUsername($username);
//判断用户名是否存在,如果不存在则输出提示消息
if (is_null($querydata)) {
echo "
<script>alert('Account is not exist');history.go(-1);</script>";
exit();
//判断密码是否正确,如果不正确则弹出提示消息
} elseif($querydata['password'] !== $password){
echo "
<script>alert('password is incorrect');history.go(-1);</script>";
exit();
//其他情况(用户名存在且密码正确)则输出恭喜+中文名+登录成功
}else{
echo ('恭喜'.$querydata['cnname'].'登录成功!');
//记录登录成功
$_SESSION['login'] = 1;
//记录谁登录成功。
$_SESSION['username'] = $querydata['cnname'];
}
}
}
}
?>
注:

这里我遇到过$_POST 获取不到表单的内容,原因是Input 标签为添加name 属性

getByUsername 方法可以获取模型中username=参数的行并返回数组。

在/php/user/tpl/login下创建模板文件

/php/user/tpl/login/index.html

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<meta name="description" content="">
<meta name="author" content="Tim zhong">
<!-- <link rel="icon" href="favicon.ico"> -->
<title>系统登录</title>
<!-- Bootstrap core CSS -->
<link href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<style>
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}
.form-signin {
max-width: 330px;
padding: 15px;
margin: 0 auto;
}
.form-signin .form-signin-heading,
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="text"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-
4000
top-right-radius: 0;
}
</style>
</head>
<body>
<div class="container">
<!-- 必须要有name属性才能获取POST -->
<form class="form-signin" name="login" method="POST" action="__URL__/chklogin" >
<h2 class="form-signin-heading">Please sign in</h2>
<label for="inputAccount" class="sr-only">Account</label>
<input type="text" name="username" id="inputAccount" class="form-control" placeholder="Account" maxlength="20" required="" autofocus="" />

<label for="inputPassword" class="sr-only">Password</label>
<input type="password" name="password" id="inputPassword" class="form-control" placeholder="Password" required=""/>
<div class="checkbox">
<label><input type="checkbox" value="remember-me">Remember me</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<input type="hidden" name="dopost" value="login" />
</form>
</div>
<!-- <include file="Common:footer" />-->
</body>
注:

未做兼容性测试

做好这几项我们就可以在浏览器中输入: http://localhost:8081/PHP/user.php/Login/
来显示默认的模板也就是登录框:



在数据库中添加数据(这步什么时候做都可以):



在登录界面输入数据库中对应的username 和password 就可以了提交登录了

注:

这里的密码是MD5加密存储的。

提交后,系统会调用chklogin 方法



到此为止我们完成了一个最最简单的登录验证。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐