douzhannao5357 2015-01-13 18:07
浏览 93
已采纳

注意:未定义的变量:第11行的C:\ wamp \ projects \ ServiceAdmin \ login \ loginauth.php中的验证码

I am currently creating a login script for a project, yet trying to introduce a captcha has proven issues with it; of which I am not entirely certain of.

Login page:

Form:

enter image description here

Form code:

    <div class="col-lg-8">
    <script src="https://www.google.com/recaptcha/api.js"></script>
      <form class="form-signin" method="post" action="loginauth.php">
        <h2 class="form-signin-heading">Sign in to ServiceAdmin</h2><br>
        <label class="sr-only">Email address</label>
        <input name="email" type="email" class="form-control" placeholder="Email address" required autofocus>
        <label class="sr-only">Password</label>
        <input name="password" type="password" class="form-control" placeholder="Password" required><br>
    </div>
    <div class="col-lg-4">
        <div class="g-recaptcha" style="margin-top: 115px; margin-left: 20px;" data-sitekey="REDACTED"></div>

        <?php if($_SESSION['login.captcha']){
            echo '<font color="red"><p style="margin-left:27px;">Please tick this checkbox to verify your security.</p></font>';
            unset($_SESSION['login.captcha']);
          } else {
            echo '<p style="margin-left:27px;">Please tick this checkbox to verify your security.</p>';
          } ?>

        </div><br><br>
        <input class="btn btn-lg btn-primary btn-block" type="submit" value="Sign in">
      </form>

Login backend code (loginauth.php):

<?php
error_reporting(E_ALL);
$email = $password = $captcha = NULL;
if(isset($_POST['email'])){
  $email = $_POST['email'];
}
if(isset($_POST['password'])){
  $password = $_POST['password'];
}
if(isset($_POST['g-recaptcha-response'])){
  $captcha = $_POST['g-recaptcha-response'];
}
if(!$captcha){
  echo "captcha error";
  exit;
}

$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=REDACTED&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false){
  "captcha error bot";
} else {
  "success";
}
?>

No matter what, despite the captcha being filled in, it will not be recognized as entered, and will come up with error as such:

( ! ) Notice: Undefined variable: captcha in C:\wamp\projects\ServiceAdmin\login\loginauth.php on line 11

If anybody has any ideas as to the cause of this issue, help would be appreciated profusely.

  • 写回答

2条回答 默认 最新

  • doutan1671 2015-01-13 18:14
    关注

    PHP throws notices if you reference a variable that hasn't been created yet, although the code still "works".

    In this case, $captcha is never instantiated because your code never reaches the line that creates it

    if(isset($_POST['g-recaptcha-response'])){ $captcha = $_POST['g-recaptcha-response']; }

    The common fix is to declare $captcha with a false or null value before you use/reference it on line 11.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体