Typecho 結構筆記
前端發送
admin/login.php
處理請求
var/Widget/Login.php
替註冊頁面添加 reCAPTCHA 驗證
admin/register.php
在 Register 類 添加 send_post 功能
private function send_post($url, $post_data){
$postdata = http_build_query($post_data);
$options = a`請輸入代碼`rray(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 10 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$recaptcha_json_result = file_get_contents($url, false, $context);
$result = json_decode($recaptcha_json_result, true);
return $result;
}
在 action 添加解析參數以及判斷異常功能
/** 截获验证异常 */
if ($error = $validator->run($this->request->from('name', 'password', 'mail', 'confirm', 'g-recaptcha-response'))) {
Cookie::set('__typecho_remember_name', $this->request->name);
Cookie::set('__typecho_remember_mail', $this->request->mail);
/** 设置提示信息 */
Notice::alloc()->set($error);
$this->response->goBack();
}
/** Google reCaptcha v2 */
$post_data = [
'secret' => 'secret_key_here',
'response' => $_POST["g-recaptcha-response"]
];
$recaptcha_result = $this->send_post('https://www.google.com/recaptcha/api/siteverify', $post_data);
返回前端文件
admin/register.php
在 form 中添加 驗證欄位, 方便 recaptcha 傳入
<p>
<div class="g-recaptcha" data-sitekey="site_key_here"></div>
</p>
在底部 script 後面加上
引用來源
https://newabug.top/archives/67.html