1、前台登录用的form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<form id='form_login' action="{:U('runLogin')}" method="post">
    <div class="row">
        <label class="field">登陆名
        </label>
        <div class="item">
            <input id="address" class="basic-input" name="uname" maxlength="70" size="46"/>
        </div>
    </div>
    <div class="row">
        <label class="field">密码
        </label>
        <div class="item">
            <input id="pwd" class="basic-input" type="password"  name="pwd" maxlength="70" size="46"/>
        </div>
    </div>
    <div class="row">
        <div class="item">
            <input type="checkbox" name='auto' class='auto' id='auto' checked='1'/>
            <label for="auto">记住我,下次自动登录</label>
        </div>
    </div>
    <hr class="hrline"/>
    <div class="row footer">
        <div class="item">
            <input class="loc-btn" type="button" id="submit_form" value="提交"/>
            <input class="lnk-flat" type="button" id="cancel_form" value="取消"/>
        </div>
    </div>
</form>

2、后台验证用户名密码,如果登录成功则写入session和cookie,如果cookie不设置失效时间,默认是关闭全部浏览器时失效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public function runLogin(){
    if(!$this->isPost()){
        halt('页面不存在');
    }
    $uname=I('post.uname');
    $pwd=I('post.pwd','','md5');
    $auto=I('post.auto');
 
    $condition = array(
        'uname' => $uname,
        'pwd'=> $pwd
        );
    $user=M('User')->where($condition)->find();
    if($user){
        $uid=$user[id];
        if($user[nickname]){
            $uname=$user[nickname];
        }
        session('uid',$user[id]);
        session('uname',$uname);
        if($auto=='on'){
            cookie('uid',$uid,30*24*3600);
            cookie('uname',$uname,30*24*3600);
        }
        header('Content-type:text/html;Charset=UTF-8');
        redirect(__APP__);
    }else{
        header('Content-type:text/html;Charset=UTF-8');
        redirect(U('login'),2,'用户名或密码错误,正在跳转回登陆页...');
    }
}

3、首页控制器判断session或者cookie是否存在

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php
// 首页控制器
class IndexAction extends Action {
    public function index(){
        $data=session('uname');
        if($data){
            $this->data = $data;
        }else if(cookie('uname')){
            $this->data = cookie('uname');
        }
        $this->display();
    }
}

4、首页页面模板中,使用标签判断用户信息,显示不同的内容

1
2
3
4
5
6
7
8
9
<div id='login' class='right'>
    <empty name="data">
            <a href='{:U('Login/login')}'>登录</a>
            <a href='{:U('Login/register')}'>注册</a>
        <else/>
            <a href='#'>欢迎 {$data}</a>
            <a href='{:U('Login/logout')}'>注销</a>
    </empty>                 
</div>

5、注销登录

1
2
3
4
5
6
7
public function logout(){
    session('uid',null);
    session('uname',null);
    cookie('uid',null);
    cookie('uname',null);
    redirect(__APP__);
}

基于ThinkPHP的开发笔记3-登录功能(转)的更多相关文章

  1. Android学习笔记_65_登录功能本身没有任何特别

    对于登录功能本身没有任何特别,使用httpclient向服务器post用户名密码即可.但是为了保持登录的状态(在各个Activity之间切换时要让网站知道用户一直是处于登录的状态)就需要进行cooki ...

  2. ThinkPHP开发笔记-用户登录注册

    1.修改模块配置,Application/当前模块名/Conf/config.php <?php return array( //数据库配置信息 'DB_TYPE' => 'mysql', ...

  3. 实现基于dotnetcore的扫一扫登录功能

    第一次写博客,前几天看到.netcore的认证,就心血来潮想实现一下基于netcore的一个扫一扫的功能,实现思路构思大概是web端通过cookie认证进行授权,手机端通过jwt授权,web端登录界面 ...

  4. 《ArcGIS Runtime SDK for .NET开发笔记》--三维功能

    介绍 在ArcGIS Runtim SDK for .NET 10.2.6中,新添加了三维地图功能.在ArcGIS中,我们将三维地图称为Scene(场景),所以在Runtime SDK SDK for ...

  5. 齐博软件 著名的老牌CMS开源系统 X1.0基于thinkphp开发的高性能免费开源PHP开放平台齐博x1.0基于thinkphp框架开发的高性能免费开源系统 主推圈子 论坛 预定拼团分销商城模块

    齐博X1--标签变量大全 1.网站名称: {$webdb.webname} 2.网址: {$webdb[www_url]} {:get_url('home')} 3.网站SEO关键词: 首页:{$we ...

  6. 基于thinkphp的uploadify上传图功能

    php Action server端 <?php /*  * To change this template, choose Tools | Templates  * and open the ...

  7. JFinal使用笔记3-注册和登录功能开发记录

    首页 开源项目 问答 代码 博客 翻译 资讯 移动开发 招聘 城市圈 当前访客身份:游客 [ 登录 | 加入开源中国 ]   当前访客身份: 游客 [ 登录 | 加入开源中国 ] 软件   土龙 关注 ...

  8. 基于SpringBoot从零构建博客网站 - 整合ehcache和开发注册登录功能

    对于程序中一些字典信息.配置信息应该在程序启动时加载到缓存中,用时先到缓存中取,如果没有命中,再到数据库中获取同时放到缓存中,这样做可以减轻数据库层的压力.目前暂时先整合ehcache缓存,同时预留了 ...

  9. 基于IntelliJ IDEA开发工具搭建SSM框架并实现页面登录功能详细讲解二

    接: 接下来配置类 UserController package com.chatRotbot.controller; import com.chatRotbot.model.User; import ...

随机推荐

  1. Linux系统下配置squid代理服务器的过程详解

    简单记录一下Squid透明代理服务器的配置 环境:VirtualBox + CentOS 6.0 + squid-3.1.4-1.el6.i686 0.检查squid是否默认安装,没有安装的先安装 [ ...

  2. Autofac property injection

    https://autofaccn.readthedocs.io/en/latest/register/prop-method-injection.html Property and Method I ...

  3. What's the difference between UTF-8 and UTF-8 without BOM?

    https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom ...

  4. hibernate——第一次简单的使用

    提前有jdk.mysql.hibernate必须jar包.mysql连接jar包 mysql中的表 Java中的bean,User类 package com.xiaostudy.demo; publi ...

  5. 多线程-栅栏CyclicBarrier

    上一篇总结了闭锁CountDownLatch,这一篇总结一下栅栏CyclicBarrier.它们两者之间的区别主要是,闭锁是等待一个事件发生,比如上一篇的田径比赛,运动员等待裁判哨声一响就可以开始跑, ...

  6. angular 图片懒加载(延迟加载)

    github 原文 https://github.com/Treri/me-lazyload me-lazyload angular 的图像资源延迟加载指令 例子(Demo) 演示网站(Demo Si ...

  7. cassandra 之 集群部署

    其实关于部署没啥好说的,修改config/cassandra.yaml以下几个地方就可以了 cluster_name: 'cluster_cassandra' data_file_directorie ...

  8. Mongodb笔记(三)user && aggregate && mapReduce

    版本:mongodb3.4. User: mongodb使用验证登录:默认不开启,mongod中使用--auth开启:  mongod -port=3000 --auth  : 基本方法: db.cr ...

  9. Linux grep 命令大全

    grep: 用于模糊查找,在标准输入或者文件中 格式: grep [选项参数]...  PATTERN |FILE ... 选项参数说明: -E, --extended-regexp PATTERN ...

  10. javascript简单介绍总结(二)

    JavaScript 函数函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块.JavaScript 函数语法函数就是包裹在花括号中的代码块,前面使用了关键词 function:function ...