Foros del Web » Programando para Internet » PHP »

Problema con CAPTCHA

Estas en el tema de Problema con CAPTCHA en el foro de PHP en Foros del Web. Estoy creando un formulario para crear cuentas y le puse para que pida un CAPTCHA para evitar spam, pero me tira error por una variable. ...
  #1 (permalink)  
Antiguo 03/11/2013, 17:52
 
Fecha de Ingreso: septiembre-2010
Mensajes: 6
Antigüedad: 13 años, 6 meses
Puntos: 0
Problema con CAPTCHA

Estoy creando un formulario para crear cuentas y le puse para que pida un CAPTCHA para evitar spam, pero me tira error por una variable. Les dejo el código.

Código:
<?php
  // Insert the page header
  $page_title = 'Sign Up';
  require_once('header.php');

  require_once('appvars.php');
  require_once('connectvars.php');

  // Connect to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (isset($_POST['submit'])) {
    // Grab the profile data from the POST
    $username = mysqli_real_escape_string($dbc, trim($_POST['username']));
    $password1 = mysqli_real_escape_string($dbc, trim($_POST['password1']));
    $password2 = mysqli_real_escape_string($dbc, trim($_POST['password2']));
	$mail = mysqli_real_escape_string($dbc, trim($_POST['mail']));
	
	// Check the CAPTCHA pass-phrase for verification
    $user_pass_phrase = sha1($_POST['verify']);
    if ($_SESSION['pass_phrase'] == $user_pass_phrase) {

    if (!empty($username) && !empty($password1) && !empty($password2) && ($password1 == $password2) && !empty($mail)){
      // Make sure someone isn't already registered using this username
      $query = "SELECT * FROM mismatch_user WHERE username = '$username'";
      $data = mysqli_query($dbc, $query);
      if (mysqli_num_rows($data) == 0) {
        // The username is unique, so insert the data into the database
        $query = "INSERT INTO mismatch_user (username, password, mail, join_date) VALUES ('$username', SHA('$password1'), '$mail', NOW())";
        mysqli_query($dbc, $query);

        // Confirm success with the user
        echo '<p>Your new account has been successfully created. You\'re now ready to <a href="login.php">log in</a>.</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        // An account already exists for this username, so display an error message
        echo '<p class="error">An account already exists for this username. Please use a different address.</p>';
        $username = "";
      }
    }
    else {
      echo '<p class="error">You must enter all of the sign-up data, including the desired password twice.</p>';
    }
  }
    else {
      echo '<p class="error">Please enter the verification pass-phrase exactly as shown.</p>';
    }
 }

  mysqli_close($dbc);
?>

  <p>Please enter your username and desired password to sign up to Mismatch.</p>
  <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <fieldset>
      <legend>Registration Info</legend>
      <label for="username">Username:</label>
      <input type="text" id="username" name="username" value="<?php if (!empty($username)) echo $username; ?>" /><br />
      <label for="password1">Password:</label>
      <input type="password" id="password1" name="password1" /><br />
      <label for="password2">Password (retype):</label>
      <input type="password" id="password2" name="password2" /><br />
      <label for="mail">Mail:</label>
      <input type="mail" id="mail" name="mail" /><br />
     <label for="verify">Verification:</label>
    <input type="text" id="verify" name="verify" value="Enter the pass-phrase." /> <img src="captcha.php" alt="Verification pass-phrase" />
    </fieldset>
    <input type="submit" value="Sign Up" name="submit" />
  </form>

<?php
  // Insert the page footer
  require_once('footer.php');
?>
Este es el del CAPTCHA. Lo probé en otro formulario más sencillo y me funcionó bien

Código:
<?php
  session_start();

  // Set some important CAPTCHA constants
  define('CAPTCHA_NUMCHARS', 6);  // number of characters in pass-phrase
  define('CAPTCHA_WIDTH', 100);   // width of image
  define('CAPTCHA_HEIGHT', 25);   // height of image

  // Generate the random pass-phrase
  $pass_phrase = "";
  for ($i = 0; $i < CAPTCHA_NUMCHARS; $i++) {
    $pass_phrase .= chr(rand(97, 122));
  }

  // Store the encrypted pass-phrase in a session variable
  $_SESSION['pass_phrase'] = sha1($pass_phrase);

  // Create the image
  $img = imagecreatetruecolor(CAPTCHA_WIDTH, CAPTCHA_HEIGHT);

  // Set a white background with black text and gray graphics
  $bg_color = imagecolorallocate($img, 255, 255, 255);     // white
  $text_color = imagecolorallocate($img, 0, 0, 0);         // black
  $graphic_color = imagecolorallocate($img, 64, 64, 64);   // dark gray

  // Fill the background
  imagefilledrectangle($img, 0, 0, CAPTCHA_WIDTH, CAPTCHA_HEIGHT, $bg_color);

  // Draw some random lines
  for ($i = 0; $i < 5; $i++) {
    imageline($img, 0, rand() % CAPTCHA_HEIGHT, CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }

  // Sprinkle in some random dots
  for ($i = 0; $i < 50; $i++) {
    imagesetpixel($img, rand() % CAPTCHA_WIDTH, rand() % CAPTCHA_HEIGHT, $graphic_color);
  }

  // Draw the pass-phrase string
  imagettftext($img, 18, 0, 5, CAPTCHA_HEIGHT - 5, $text_color, 'Courier New Bold.ttf', $pass_phrase);

  // Output the image as a PNG using a header
  header("Content-type: image/png");
  imagepng($img);

  // Clean up
  imagedestroy($img);
?>
  #2 (permalink)  
Antiguo 03/11/2013, 17:54
Avatar de pateketrueke
Modernizr
 
Fecha de Ingreso: abril-2008
Ubicación: Mexihco-Tenochtitlan
Mensajes: 26.399
Antigüedad: 16 años
Puntos: 2534
Respuesta: Problema con CAPTCHA

¿Está dentro de tus facultades compartir el mensaje de error completo o es nuestra obligación adivinarlo leyendo tu código?

Porque dudo que alguien sepa que variable es la que te da error, ¿o tu que harías en nuestro lugar?
__________________
Y U NO RTFM? щ(ºдºщ)

No atiendo por MP nada que no sea personal.
  #3 (permalink)  
Antiguo 03/11/2013, 21:07
 
Fecha de Ingreso: septiembre-2010
Mensajes: 6
Antigüedad: 13 años, 6 meses
Puntos: 0
Respuesta: Problema con CAPTCHA

Cita:
Iniciado por pateketrueke Ver Mensaje
¿Está dentro de tus facultades compartir el mensaje de error completo o es nuestra obligación adivinarlo leyendo tu código?

Porque dudo que alguien sepa que variable es la que te da error, ¿o tu que harías en nuestro lugar?
Me olvide de ponerlo, lo tengo en locahost y estaba en otra computadora

Notice: Undefined variable: _SESSION in ...signup.php on line 21
  #4 (permalink)  
Antiguo 04/11/2013, 09:04
 
Fecha de Ingreso: octubre-2012
Mensajes: 135
Antigüedad: 11 años, 6 meses
Puntos: 8
Respuesta: Problema con CAPTCHA

falta session_start(); en el php del formulario

Etiquetas: captcha, formulario, mysql, select, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 14:45.