You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP中Google reCAPTCHA无法正确放置至指定位置问题

解决Google reCAPTCHA位置异常问题

嘿,我看你遇到了reCAPTCHA位置跑偏的问题——它现在跑到输入框顶上去了,本该在密码输入框和提交按钮中间对吧?我帮你瞅了下代码,核心问题是HTML表格的结构错误导致布局混乱,咱们一步步来修复:

问题根源

你代码里密码输入框对应的<tr>标签没有闭合,而且reCAPTCHA的<div>直接塞在了一个未正确闭合的<tr>里,浏览器为了修复错误的HTML结构,就把reCAPTCHA的位置搞乱了。另外,验证请求的URL里还少了一个关键的等号,这个也得一起补上,不然后续验证会失败。

修复步骤

  • 首先,把密码输入行的<tr>标签正确闭合,补上</tr>
  • 给reCAPTCHA单独添加一行表格行(<tr>),让它位于密码输入行和提交按钮行之间,并且用colspan="2"让它跨两列,保证显示整齐
  • 把reCAPTCHA的引入脚本移到<head>标签里,避免干扰表格布局
  • 补全验证请求URL里的response参数等号

修复后的完整代码

<?php 
$public_key=""; 
$private_key=""; 
$url="https://www.google.com/recaptcha/api/siteverify"; 

if(array_key_exists('submit_form',$_POST)) { 
    $response_key=$_POST['g-recaptcha-response']; 
    // 补全了response参数后的等号
    $response=file_get_contents($url.'?secret='.$private_key.'&response='.$response_key.'&remoteip='.$_SERVER['REMOTE_ADDR']); 
    $response=json_decode($response); 
} 
?>
<!DOCTYPE html>
<html>
<head>
<title>Authentification</title>
<!-- 把reCAPTCHA脚本移到head中,避免干扰表格布局 -->
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<form method="post" action="auth.php">
<center>
<fieldset style="width:300px;">
<legend align='center'><h1>Connexion</h1></legend>
<table>
<tr>
<td><b>Email: </b></td>
<td><input type="email" name="email" required=""></td>
</tr>
<tr>
<td><b>Mot de passe: </b></td>
<td><input type="password" name="password" required=""></td>
</tr> <!-- 补上了缺失的tr闭合标签 -->
<tr> <!-- 给reCAPTCHA单独新增一行 -->
<td colspan="2"> <!-- 跨两列让reCAPTCHA居中显示 -->
<div class="g-recaptcha" data-sitekey="<?php print $public_key; ?>"></div>
</td>
</tr>
<tr>
<td><input type="submit" name="submit_form" value="Se Connecter"></td>
<td><a href="ajout.php"><button type="button">S'inscrire</button></a></td>
</tr>
</table>
</fieldset>
</center>
</form>
</body>
</html>

现在reCAPTCHA应该会乖乖显示在密码输入框和提交按钮之间了,而且验证请求也能正常发送啦~

内容的提问来源于stack exchange,提问作者user13722137

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:35:31