Typecho添加验证码

http://homeway.me


0x01.关于

写Typecho主题时候碰到的问题,在登录时候添加验证码。

Typecho有很好的路由机制,并且MVC模型模块化做的很好。

系统代码在/var/中,包括了:Widget工具,Typecho模型等。

开发者二次开发代码在/usr/中,主要放置Theme主题,Plugins插件等。

添加验证码不能用插件形式,因此只能直接在源码中更改。

这里说两种验证码方式,Geetest极验验证,和用php生成图片两种方法。



0x02.Geetest极验

这个简单,到http://geetest.com/官网注册下账号。

1、按照极验的要求,修改前端代码,即,在/admin/login.php中添加代码。

2、添加Geetest插件,在/var/Widget/中添加Geetestlib.php

3、修改后台验证端,修改/var/Widget/Login.php如下:

require_once('Geetestlib.php');
class Widget_Login extends Widget_Abstract_Users implements Widget_Interface_Do {
    public function action() {
        /*  此处...省略...字 */

        /* 极验代码 */
        $captcha_id = "xxxxxxxxxxxxx";//TODO: replace it with your own captcha's id
        $private_key = "xxxxxxxxxxxxx";//TODO: replace it with your own captcha's key
        $geetestlib = new GeetestLib($captcha_id,$private_key);
        if (isset($_POST['geetest_challenge']) && isset($_POST['geetest_validate']) &&  isset($_POST['geetest_seccode'])) {
            $validate_response = $geetestlib->geetest_validate(@$_POST['geetest_challenge'], @$_POST['geetest_validate'], @$_POST['geetest_seccode']);
        }else{
            $this->widget('Widget_Notice')->set("use your own captcha validate ");
            $this->response->goBack();
        }
        if ($validate_response == TRUE) {
        } else if ($validate_response == FALSE) {
            $this->widget('Widget_Notice')->set('请先验证');
            $this->response->goBack();
        }else{
            $this->widget('Widget_Notice')->set('FORBIDDEN');
            $this->response->goBack();
        }
        /*  此处...省略...字 */
 }

...部分代码,详细下载链接如下:

http://homeway.me/code/typecho-Login-1.php



0x02.php生成图片

1、修改前端代码,修改/admin/login.php,添加如下:

    <p>
        <img style="cursor:pointer" title="刷新验证码" id="refresh" border='0' src='verify.php' onclick="document.getElementById('refresh').src='typecho-code.php?t='+Math.random()"/>
    </p>
    <p>
        <label for="name" class="sr-only"><?php _e('验证码'); ?></label>
        <input type="text" id="name" name="verify" value="" placeholder="<?php _e('验证码'); ?>" class="text-l w-100" />
    </p>

2、添加php生成验证码的php插件,添加到/admin/typecho-code.php/admin/typecho-t1.ttf,代码下载:

http://homeway.me/code/typecho-code.php

http://homeway.me/code/typecho-t1.ttf

3、修改后台验证代码,修改/var/Widget/Login.php如下:

session_start();
$flag=true;
class Widget_Login extends Widget_Abstract_Users implements Widget_Interface_Do {
    public function action() {
        /*  此处...省略...字 */
        if (strtolower($_POST['verify']) != strtolower($_SESSION['verify']) ) {
            /** 防止穷举,休眠3秒 */
            sleep(3);
            $this->widget('Widget_Notice')->set(_t('验证码错误!'), 'error');
            $this->response->goBack('?referer=' . urlencode($this->request->referer));
            $flag = false;
        }else{
            /** 如果验证码对了,开始验证用户 **/
            $valid = $this->user->login($this->request->name, $this->request->password,
            false, 1 == $this->request->remember ? $this->options->gmtTime + $this->options->timezone+30*24*3600 : 0);
            /** 比对密码 */
            if (!$valid) {
                /** 防止穷举,休眠3秒 */
                sleep(3);
                $this->pluginHandle()->loginFail($this->user, $this->request->name,
                $this->request->password, 1 == $this->request->remember);
                Typecho_Cookie::set('__typecho_remember_name', $this->request->name);
                $this->widget('Widget_Notice')->set(_t('用户名或密码无效'), 'error');
                $this->response->goBack('?referer=' . urlencode($this->request->referer));
            }
            $this->pluginHandle()->loginSucceed($this->user, $this->request->name,
            $this->request->password, 1 == $this->request->remember);
        }
        /*  此处...省略...字 */
    }
}

...部分代码,详细下载链接如下:

http://homeway.me/code/typecho-Login-2.php




本文出自 夏日小草,转载请注明出处: http://homeway.me/2015/03/25/add-check-code-for-typecho/
by 小草

2015-03-25 14:49:20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值