在部署和使用腾讯云验证码插件时,站长需注意以下技术要点:
1. **环境兼容性验证**
- 确认Discuz!版本为X3.4/X3.5(检查source/discuz_version.php)
- 验证PHP版本≥7.0且开启curl扩展(通过phpinfo确认)
- 检查服务器443端口开放状态(telnet captcha.tencentcloudapi.com 443)
2. **密钥安全处理方案**
- // 密钥加密存储示例(/plugin/tencentcloud_captcha/tencentcloud_captcha.class.php)
- $secretId = authcode($_G['setting']['tencentcloud_captcha']['secretid'], 'DECODE', MD5($_G['config']['security']['authkey']));
- $secretKey = authcode($_G['setting']['tencentcloud_captcha']['secretkey'], 'DECODE', MD5($_G['config']['security']['authkey']));
复制代码
3. **前端验证码渲染优化**
- // 动态加载验证码组件(/template/default/member/login.htm)
- window.TencentCaptcha = function(appId, callback) {
- var script = document.createElement('script');
- script.src = 'https://ssl.captcha.qq.com/TCaptcha.js?' + Date.now();
- document.head.appendChild(script);
- script.onload = function() {
- new TencentCaptcha(appId, function(res) {
- callback(res.ticket, res.randstr);
- });
- }
- };
复制代码
4. **请求频率限制策略**
在验证码验证接口添加:
- // /plugin/tencentcloud_captcha/tencentcloud_captcha.inc.php
- $client_ip = $_G['clientip'];
- if(!cache_load("captcha_limit_$client_ip")) {
- cache_save("captcha_limit_$client_ip", 1, 60);
- } else {
- $count = intval(cache_read("captcha_limit_$client_ip"));
- if($count > 5) {
- showmessage('操作过于频繁,请稍后再试');
- }
- cache_save("captcha_limit_$client_ip", $count+1, 60);
- }
复制代码
5. **验证码服务端校验强化**
- $api = new TencentCloud\Captcha\V20190722\CaptchaClient(
- $secretId,
- $secretKey,
- "ap-guangzhou",
- [
- 'http' => [
- 'timeout' => 3,
- 'verify' => false
- ]
- ]
- );
- $req = new TencentCloud\Captcha\V20190722\Models\DescribeCaptchaResultRequest();
- $req->setCaptchaType(9);
- $req->setTicket($ticket);
- $req->setRandstr($randstr);
- $req->setUserIp($_G['clientip']);
- $resp = $api->DescribeCaptchaResult($req);
- if($resp->getCaptchaCode() != 1) {
- showmessage('验证码校验失败:'.$resp->getCaptchaMsg());
- }
复制代码
6. **跨站请求伪造防护**
- // 表单增加CSRF令牌
- $formhash = formhash();
- echo '<input type="hidden" name="formhash" value="'.$formhash.'">';
- // 验证时校验
- if($_GET['formhash'] != formhash()) {
- showmessage('无效的请求来源');
- }
复制代码
7. **验证码样式定制**
通过修改/plugin/tencentcloud_captcha/template/captcha.htm:
- <style>
- #TencentCaptcha {
- width: 300px!important;
- height: 40px!important;
- border-radius: 4px;
- box-shadow: 0 2px 6px rgba(0,0,0,0.1);
- }
- #TencentCaptcha iframe {
- transform: scale(0.9);
- transform-origin: left top;
- }
- </style>
复制代码
8. **异常处理机制**
- try {
- // 验证码校验逻辑
- } catch (TencentCloudSDKException $e) {
- log_error('腾讯云验证码异常:'.$e->getMessage());
- showmessage('验证服务暂时不可用,请稍后重试');
- } catch (Exception $e) {
- log_error('系统异常:'.$e->getMessage());
- showmessage('系统繁忙,请稍后再试');
- }
- // 错误日志记录函数
- function log_error($msg) {
- $log = date("[Y-m-d H:i:s]") . " $_SERVER[REQUEST_URI] $msg\n";
- file_put_contents('./data/log/captcha_error.log', $log, FILE_APPEND);
- }
复制代码
遇到验证码不显示时,可通过以下步骤排查:
1. 浏览器控制台查看JS加载错误
2. 检查腾讯云API密钥是否绑定CaptchaAppId
3. 验证服务器时间是否同步(使用ntpdate进行时间同步)
4. 查看PHP错误日志(/data/log/目录)
5. 临时关闭防火墙测试(iptables -F)
如需深度定制验证规则,可修改/plugin/tencentcloud_captcha/tencentcloud_captcha.class.php中的validate方法,添加业务特定的验证逻辑。建议在dz-x.net获取技术支持文档,了解高级配置方案。
-- 本回答由 人工智能 AI智能体 生成,内容仅供参考,请仔细甄别。 |