Jump to content

Help with inserting math captcha in registration form


sofia403

Recommended Posts

Hi, im trying to have a math captcha in my registration form, but having trouble with setting it up in my form. sorry for the large code. If i change the value of $_POST['Submit'] to something else like $_POST['Submit1'] and then same for the math image captcha then it works, but i would like it to work as part of the form, makes sense? lol right now the form just posts the value and doesnt check for captcha values! if someone could help me out here that would be great! thank you.

<?php 
if(isset($_POST['Submit'])){
if($_POST['Submit'] != $_SESSION['security_number'])
      {
         $error = "";
      }
      else
      {
         $error = "";
      }
   
//NEED TO CHECK IF FIELDS ARE FILLED IN
if( empty($_POST['name']) && (empty($_POST['email']))){
header("Location:Messages.php?msg=3"); 
exit();
}
if( empty($_POST['pw1']) && (empty($_POST['pw2']))){
header( "Location:Messages.php?msg=4" ); 
exit();
}
$name=$_POST['name'];
$email=$_POST['email'];

$pw1=$_POST['pw1'];
$pw2=$_POST['pw2'];

if("$pw1" !== "$pw2"  ){
header( "Location:Messages.php?msg=5" ); 
exit();
}
$ip = $_SERVER['REMOTE_ADDR'];

//connect to the db server , check if uname exist
include('config.php');
$query1=("Select * from user where email='$email'");
$result1= mysql_query($query1); 
$num1=mysql_num_rows($result1);
if ($num1 > 0) {//Email already been used
header( "Location:Messages.php?msg=11" ); 
exit();
}else{
$query=("Select * from user where uname='$name'");
$result= mysql_query($query); 
$num=mysql_num_rows($result);
if ($num > 0) {//Username already exist
header( "Location:Messages.php?msg=6" ); 
exit();
}else{
//if username does not exist insert user details
$query=( "INSERT INTO user (uname, pw,email,date_joined,ip,level) VALUES ('$name',md5('$pw1'),'$email',NOW(),'$ip','Normal')");
if (@mysql_query ($query)) {
header("location:login.php?reg=1");
exit;
}
}
}
mysql_close();
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/Auth.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Registration</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<link href="styleLog.css" rel="stylesheet" type="text/css">
<script language="javascript" type="text/javascript">
   function reloadCaptcha()
   {
      document.getElementById('captcha').src = document.getElementById('captcha').src+ '?' +new Date();
   }
</script>
</head>

<body>
<table width="100%"  border="0" cellspacing="7" cellpadding="0">
  <tr class="temptitle">
    <td><!-- InstanceBeginEditable name="EditRegion4" -->New User Registration <!-- InstanceEndEditable --></td>
  </tr>
  <tr>
    <td><!-- InstanceBeginEditable name="EditRegion3" -->
   <form name="form1" action="register.php" method="post">
   <table width="657" border="0">
  <tr>
    <td width="122"><div align="left">Name</div></td>
    <td width="525"><input name="name" type="text" size="40"></td>
  </tr>
  <tr>
    <td><div align="left">Email</div></td>
    <td><input name="email" type="text" size="40"></td>
  </tr>
  <tr>
    <td><div align="left">Password</div></td>
    <td><input name="pw1" type="password" size="40"></td>
  </tr>
  <tr>
    <td ><div align="left">Confirm Password </div></td>
    <td><input name="pw2" type="password" size="40"></td>
  </tr>
<tr>
   <td><img src="math_captcha/image.php" alt="Click to reload image" title="Click to reload image" id="captcha" onclick="javascript:reloadCaptcha()" /></td>
   <td><input type="text" name="Submit" value="what's the result?" onclick="this.value=''" /></td>
   <td>
</tr>
  <tr>
    <td></td>
    <td> <input name="Submit" type="submit" value="Register"></td>
  </tr>
</table>
</form>
<?=$error?>

 

MOD EDIT: code tags added.

Link to comment
Share on other sites

The issue that you are facing due to duplicate form element name "Submit" for both the submit button and captcha input text. The submit button overrides the value of captcha field.

 

Change

<td><input type="text" name="Submit" value="what's the result?" onclick="this.value=''" /></td>

 

To

<td><input type="text" name="security" value="what's the result?" onclick="this.value=''" /></td>

 

Also, change

if($_POST['Submit'] != $_SESSION['security_number'])

 

To

if($_POST['security'] != $_SESSION['security_number'])

Link to comment
Share on other sites

thanks anupamsaha,

 

thats what i figure too, im just unclear how i can actually submit both the form and my math captcha  together when i hit "SUBMIT"

 

i can choose either "captcha" or my registration form but not both of them. i would greatly appreciate if someone could check the bloew code for me.

 

this is what im working with- im using two files that i got INDEX.PHP and IMAGE.PHP which both work fine on their own, and  im having trouble integrating them to work with my registration form.  sorry for the large code...thank you.

 

INDEX.PHP
-------------------------------------------------------------------------------------------------------
<?php
session_start();
if( isset( $_POST['secure'] ) )
{
if($_POST['secure'] != $_SESSION['security_number'])
{
$error = "OOOK! Here's what you must do: click Start -> Run and write calc.";
}
else
{
$error = "Man, you're good! Your result is correct.";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP math captcha</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<script language="javascript" type="text/javascript">
/* this is just a simple reload; you can safely remove it; remember to remove it from the image too */
function reloadCaptcha()
{
document.getElementById('captcha').src = document.getElementById('captcha').src+ '?' +new Date();
}
</script>
</head>

<body>
<div id="container">

<h1>PHP Math captcha image</h1>

<strong>Demo</strong>
<form method="post" action="">
<input type="text" name="secure" value="what's the result?" onclick="this.value=''" />
<input type="submit" value="am I right?" /><br />
<span class="explain">click on the image to reload it</span>
<img src="image.php" alt="Click to reload image" title="Click to reload image" id="captcha" onclick="javascript:reloadCaptcha()" />
</form>
<?=$error?>
</div>
</body>
</html>


IMAGE.PHP
-----------------------------------------------------------------------------------------------------
<?php
session_start();

/*===============================================================
General captcha settings
===============================================================*/
// captcha width
$captcha_w = 150;
// captcha height
$captcha_h = 50;
// minimum font size; each operation element changes size
$min_font_size = 12;
// maximum font size
$max_font_size = 18;
// rotation angle
$angle = 20;
// background grid size
$bg_size = 13;
// path to font - needed to display the operation elements
$font_path = 'fonts/courbd.ttf';
// array of possible operators
$operators=array('+','-','*');
// first number random value; keep it lower than $second_num
$first_num = rand(1,5);
// second number random value
$second_num = rand(6,11);

/*===============================================================
From here on you may leave the code intact unless you want
or need to make it specific changes.
===============================================================*/

shuffle($operators);
$expression = $second_num.$operators[0].$first_num;
/*
operation result is stored in $session_var
*/
eval("\$session_var=".$second_num.$operators[0].$first_num.";");
/*
save the operation result in session to make verifications
*/
$_SESSION['security_number'] = $session_var;
/*
start the captcha image
*/
$img = imagecreate( $captcha_w, $captcha_h );
/*
Some colors. Text is $black, background is $white, grid is $grey
*/
$black = imagecolorallocate($img,0,0,0);
$white = imagecolorallocate($img,255,255,255);
$grey = imagecolorallocate($img,215,215,215);
/*
make the background white
*/
imagefill( $img, 0, 0, $white );
/* the background grid lines - vertical lines */
for ($t = $bg_size; $t<$captcha_w; $t+=$bg_size){
imageline($img, $t, 0, $t, $captcha_h, $grey);
}
/* background grid - horizontal lines */
for ($t = $bg_size; $t<$captcha_h; $t+=$bg_size){
imageline($img, 0, $t, $captcha_w, $t, $grey);
}

/*
this determinates the available space for each operation element
it's used to position each element on the image so that they don't overlap
*/
$item_space = $captcha_w/3;

/* first number */
imagettftext(
$img,
rand(
$min_font_size,
$max_font_size
),
rand( -$angle , $angle ),
rand( 10, $item_space-20 ),
rand( 25, $captcha_h-25 ),
$black,
$font_path,
$second_num);

/* operator */
imagettftext(
$img,
rand(
$min_font_size,
$max_font_size
),
rand( -$angle, $angle ),
rand( $item_space, 2*$item_space-20 ),
rand( 25, $captcha_h-25 ),
$black,
$font_path,
$operators[0]);

/* second number */
imagettftext(
$img,
rand(
$min_font_size,
$max_font_size
),
rand( -$angle, $angle ),
rand( 2*$item_space, 3*$item_space-20),
rand( 25, $captcha_h-25 ),
$black,
$font_path,
$first_num);

/* image is .jpg */
header("Content-type:image/jpeg");
/* name is secure.jpg */
header("Content-Disposition:inline ; filename=secure.jpg");
/* output image */
imagejpeg($img);
?>



my registration form REGISTER.PHP
--------------------------------------------------------------------------------------------------------

<?php
if(isset($_POST['Submit'])){
//NEED TO CHECK IF FIELDS ARE FILLED IN
if( empty($_POST['name']) && (empty($_POST['email']))){
header("Location:Messages.php?msg=3");
exit();
}
if( empty($_POST['pw1']) && (empty($_POST['pw2']))){
header( "Location:Messages.php?msg=4" );
exit();
}
$name=$_POST['name'];
$email=$_POST['email'];

$pw1=$_POST['pw1'];
$pw2=$_POST['pw2'];

if("$pw1" !== "$pw2" ){
header( "Location:Messages.php?msg=5" );
exit();
}
$ip = $_SERVER['REMOTE_ADDR'];

//connect to the db server , check if uname exist
include('config.php');
$query1=("Select * from user where email='$email'");
$result1= mysql_query($query1);
$num1=mysql_num_rows($result1);
if ($num1 > 0) {//Email already been used
header( "Location:Messages.php?msg=11" );
exit();
}else{
$query=("Select * from user where uname='$name'");
$result= mysql_query($query);
$num=mysql_num_rows($result);
if ($num > 0) {//Username already exist
header( "Location:Messages.php?msg=6" );
exit();
}else{
//if username does not exist insert user details
$query=( "INSERT INTO user (uname, pw,email,date_joined,ip,level) VALUES ('$name','$pw1','$email',NOW(),'$ip','Normal')");
if (@mysql_query ($query)) {
header("location:login.php?reg=1");
exit;
}
}
}
mysql_close();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><!-- InstanceBegin template="/Templates/Auth.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>Registration</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
<link href="styleLog.css" rel="stylesheet" type="text/css">
</head>

<body>
<table width="100%" border="0" cellspacing="7" cellpadding="0">
<tr class="temptitle">
<td><!-- InstanceBeginEditable name="EditRegion4" -->New User Registration <!-- InstanceEndEditable --></td>
</tr>
<tr>
<td><!-- InstanceBeginEditable name="EditRegion3" -->
<form name="form1" action="register.php" method="post">
<table width="657" border="0">
<tr>
<td width="122"><div align="left">Name</div></td>
<td width="525"><input name="name" type="text" size="40"></td>
</tr>
<tr>
<td><div align="left">Email</div></td>
<td><input name="email" type="text" size="40"></td>
</tr>
<tr>
<td><div align="left">Password</div></td>
<td><input name="pw1" type="password" size="40"></td>
</tr>
<tr>
<td ><div align="left">Confirm Password </div></td>
<td><input name="pw2" type="password" size="40"></td>
</tr>
<tr>
<td></td>
<td> <input name="Submit" type="submit" value="Register"></td>
</tr>
</table>
</form>
<!-- InstanceEndEditable --></td>
</tr>
<tr>

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.