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. Redis 后台运行

    编辑配置文件 vim {redis_home}/redis.conf 修改daemonize  (默认为no,修改为yes) 启动redis{redis_home}/src/redis-server ...

  2. quartz(6)--集群

    Quartz应用能被集群,是水平集群还是垂直集群取决于你自己的需要.集群提供以下好处: · 伸缩性 · 高可用性 · 负载均衡 目前,Quartz只能借助关系数据库和JDBC作业存储支持集群. qua ...

  3. quartz(3)--spring整合quartz入门案例

    第一步:导入jar <!-- quartz --> <dependency> <groupId>org.quartz-scheduler</groupId&g ...

  4. DataStage 服务启动

    说明:①如果发现was.datastage已经自启动,但db2没有自启动,应先将它们停止,在按顺序启动;②如果都自启动了,就不用再启动,关闭防火墙即可. --0.关闭防火墙service iptabl ...

  5. Android各种屏幕分辨率(VGA、HVGA、QVGA、WQVGA、WVGA、FWVGA) 详解 .

    http://blog.csdn.net/lucherr/article/details/8498400 看资料的时候经常看到各种VGA,全都混了,无奈,找了些资料总结了下,分享给大家: 这些术语都是 ...

  6. ASCII_01

    1.来自“http://baike.baidu.com/link?url=WgFPtGe-rT6x6X0r_OiHGVZAV87Fu4_P5fvr7FsGyrm8QqTGuvVUfg4Jx7Rn-Le ...

  7. virtio guest side implementation: PCI, virtio device, virtio net and virtqueue

    With the publishing of OASIS virtio specification version 1.0, virtio made another big step in becom ...

  8. redis、kafka、rabittMQ对比 (转)

    本文不对三者之间的性能进行对比,只是从三者的特性上区分他们,并指出三者的不用应用场景. 1.publish/subscribe 发布订阅模式如下图所示可以具有多个生产者和发布者,redis.kafka ...

  9. 三十八 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)介绍以及安装

    elasticsearch(搜索引擎)介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticse ...

  10. set, map, string, find(), string name[100],等的混合

    Unrequited Love Time Limit: 16 Seconds      Memory Limit: 131072 KB There are n single boys and m si ...