Error in captcha

0

I have the following error in captcha:

  

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

and also it is only returning me the same one if it gives an F5 in the page, the session_start(); is already in the 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><scriptsrc="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);
    ?>
    
asked by anonymous 25.11.2017 / 09:22

1 answer

1

This is a notice and not a 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 it is best to check if the submission was done by POST, you can use isset , empty or filter_input note that your if/else are running 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><scriptsrc="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">
    
25.11.2017 / 14:48