Error in captcha

Asked

Viewed 278 times

0

I have the following error in the captcha:

Notice: Undefined index: captcha in C: xampp htdocs darknetwork index.php on >line 7

and also it is only returning me the Else even if you give a F5 on the page, the session_start(); is already in config.php

  • index php.

    <?php
    require 'config.php';
    require 'classes/usuarios.class.php';
    $ip = $_SERVER["REMOTE_ADDR"];
    
    $u = new Usuarios;
    if($_SESSION['captcha'] == $_POST['captcha']) {
    
      if(isset($_POST['email']) and !empty($_POST['email'])) {
        $email = addslashes($_POST['email']);
        $senha = addslashes($_POST['senha']);
    
      if($u->login($email, $senha)) {
    
        header("Location: central.php");
    
      } else {
      ?>
    
      <div align="center" class="alert alert-danger">
      E-mail e/ou Senha incorretos, tente novamente !
      </div>
    
      <?php
              }
          }
      } else {
      ?>
    
      <div align="center" class="alert alert-danger">
      Captcha esta incorreto !
      </div>
      <?php
      }
       ?>
    
    <!DOCTYPE html>
    <html>
    <head>
        <title>Dark Network</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="assets/css/style.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
    <div class="login-page">
      <div class="form">
      <div style="color:#fff;" class="h4">
          ÁREA DE LOGIN - DARK NETWORK
        </div>
        <form class="login-form" method="post">
          <hr>
          <input type="email" name="email" placeholder="E-mail" required=""/>
          <input type="password" name="senha" placeholder="Senha" required=""/><br/><br/>
          <img src="captcha.php" alt="Código captcha"/><br/><br/>
          <input type="text" name="captcha" placeholder="Digite o código" required=""/><br/>
          <button type="submit"><span>Entrar</span></button>
          <hr>
          <p class="message">Não é registrado ? <a href="cadastrar.php">Cadastre-se aqui.</a></p>
          <p style="color:#999;">Desenvolvido por: Weltec</p>
        </form>
    </div>
    

    `

  • captcha.php

    <?php
    session_start();
    
    $codigoCaptcha = substr(md5(time().rand(0,999)), 0, 9);
    
    $_SESSION['captcha'] = $codigoCaptcha;
    
    $imagemCaptcha = imagecreatefrompng("fundocaptch.png");
    $fonteCaptcha = imageloadfont("anonymous.gdf");
    $corCaptcha = imagecolorallocate($imagemCaptcha, 0, 115, 50);
    
    imagestring($imagemCaptcha, $fonteCaptcha, 15, 5, $codigoCaptcha, $corCaptcha);
    imagepng($imagemCaptcha);
    imagedestroy($imagemCaptcha);
    ?>
    
  • Notice: Undefined index: captcha in C:\xampp\htdocs\darknetwork\index.php on >line 7 this is because php is looking for a value for the validation of your captcha even before you even type in the form to solve this problem put if(isset($_POST['submit'])){//codigo de validação aqui} and also you will have to give a name for your Submit input <button type="submit" name="submit"><span>Entrar</span></button>

  • another detail its validation is incorrect because its $_SESSION['captcha'] is saving a value in MD5 and your $_POST['captcha'] not when the two are compared will never be valid because one will always be different from the other even if the user type correctly in the form so you should color this way in the beautiful code if($_SESSION['captcha'] == MD5($_POST['captcha'])) {

  • but I would recommend you use the google reCaptcha is more robust and safer https://answall.com/questions/59952/howto implementar-recaptcha-do-google-no-meu-site

1 answer

1

That’s a notice and not a good erro to know the difference see the link quoted by @Everson:

What is the difference between "notice" and "Warning" in PHP?

However, even though it is a notice the ideal is to check if the shipment was made by POST, can use isset, empty or filter_input, note that your if/else are being executed at all times.

Important details:

  • If you use isset or empty
  • isset can be used with more than one field

a simple example would be this:

<?php
require 'config.php';
require 'classes/usuarios.class.php';
$ip = $_SERVER["REMOTE_ADDR"];

//Uma variavel para o erro
$erro = null;

$u = new Usuarios;

//Checa se todos campos vieram do form
if (isset($_POST['captcha'], $_POST['captcha'], $_POST['email'], $_POST['senha'])) {
    if ($_SESSION['captcha'] == $_POST['captcha']) {
        $email = addslashes($_POST['email']);
        $senha = addslashes($_POST['senha']);

        if($u->login($email, $senha)) {

            header("Location: central.php");
            exit; //Isso impede de "baixar" o resto

        } else {
            $erro = 'E-mail e/ou Senha incorretos, tente novamente !';
        }
    } else {
        $erro = 'Captcha esta incorreto !';
    }
}

<!DOCTYPE html>
<html>
<head>
    <title>Dark Network</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

<?php if ($erro) { /*se $erro não for null então é exibido */ ?>

<div align="center" class="alert alert-danger"><?php $erro; ?></div>

<?php } ?>

<div class="login-page">
  <div class="form">
  • 1

    Related: https://answall.com/q/186344/57801

  • @Everson corrected, was not warn was notice the correct to say, thank you. Due credits to you in reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.