Jump to content

Captcha Help


jamesjmann

Recommended Posts

I want to build a captcha from scratch, as I'm all about doing things myself, and I've proposed a nice idea on how I can achieve that, but I'm sort of lost...

 

What I want to do is write a script that will generate a random string of both letters and numbers, with the ability to set a limit on how many characters are generated.  Then, I want to store that string within a variable called "random_string".

 

Once the user types in that code, their input would get captured in a separate php file and stored within a variable like so:

$captcha_code = $_POST['catpcha_code'];

 

Then, I would write a code to see if the user's input matches the random character string from the html:

<?php
if ($random_string == $captcha_code) {
mail($to, $subject, $body);
} else {
echo ("The code you entered did not match the captcha. Please go back and try again.");
}
?>

 

Is this possible? I haven't yet tried it myself, because I don't know how to generate random strings of characters, but I was just wondering if perhaps I'm on the right track? Any suggestions or advice is appreciated =)

Link to comment
https://forums.phpfreaks.com/topic/228823-captcha-help/
Share on other sites

Ok this is what I have so far:

        <?php
  
  if (isset($_POST['submit'])) {
  $to = "myemailaddress";
  $name = $_REQUEST['name'] ;
  $email = $_REQUEST['email'] ;
  $message = $_REQUEST['message'] ;
  $subject = $_REQUEST['subject'] ;
  $header = "Contact Form";
  $captcha = $_REQUEST['captcha'];
  $browser = $_SERVER['HTTP_USER_AGENT'];
  $ip = $_SERVER['REMOTE_ADDR'];
  $reason = $_REQUEST['reason'];
  $admin = "MyName";
for($i=0; $i<10; $i++){
    $random_string .= chr(rand(0,25)+65);
}
  
  if ($captcha != $random_string) {
echo("The code you entered for the captcha was incorrect. Please go back and try again.");
  } else {
    mail($to, $header, "DJSmiley.Net Feedback\n\nVisitor Information:\n$name\n$email\n$ip\n$browser\n\nMessage - $subject\n$name says:\n$message");
mail($email, $admin, "Thank you for your feedback! I will try to get back to you with a reply when I can! \n\nTil' then \n-DJ Smiley");
    echo( "Thank you for your feedback!" );
  }
} else {
?>
<h2><a name="Feedback" id="Feedback"></a>Feedback</h2>
         <form name="contact" method="post" action="<?php $_SERVER['PHP_SELF']?>">
          <table width="100%" border="0">
            <tr>
              <td width="27%">Name:</td>
              <td width="73%"><label>
                </label>
                <label>
                <input name="name2" type="text" class="commentBoxforms" id="name2" />
                </label></td>
            </tr>
            <tr>
              <td>Email: </td>
              <td>
                  <input name="email2" type="text" class="commentBoxforms" id="email2" />
                </td>
            </tr>
            <tr>
              <td>Subject:</td>
              <td><input name="subject" type="text" class="commentBoxforms" id="subject" /></td>
            </tr>
            <tr>
              <td>Message:</td>
              <td><label>
                  <textarea name="message" cols="45" rows="5" class="commentField1" id="message"></textarea>
                  <br />
                  </label></td>
            </tr>
            <tr>
              <td>Reason For Contacting:</td>
              <td><table width="50%" border="0">
                <tr>
                  <td><input name="reason" type="radio" id="endearment" value="endearment" checked="checked" /></td>
                  <td><label for="endearment">Endearment</label></td>
                  <td><input type="radio" name="reason" id="question" value="question"/></td>
                  <td width="43%"><label for="question">Question</label></td>
                </tr>
                <tr>
                  <td width="11%"><input type="radio" name="reason" id="general" value="general" /></td>
                  <td width="37%"><label for="general">General</label></td>
                  <td width="9%"><input type="radio" name="reason" id="suggestion" value="suggestion"/></td>
                  <td><label for="suggestion">Suggestion</label></td>
                </tr>
                <tr>
                  <td><input type="radio" name="reason" id="professional" value="professional" /></td>
                  <td><label for="professional">Professional</label></td>
                  <td><input type="radio" name="reason" id="technical" value="technical"/></td>
                  <td><label for="technical">Technical</label></td>
                </tr>

              </table></td>
            </tr>
            <tr>
              <td><p>Captcha:</p>
                <p>Are you human?<br />
                  Enter the following:</p>
                <p><?php
echo "<p class=\"captcha\">" .$random_string. "</p>";
?></p>
                <p>*case sensitive</p></td>
              <td><input name="captcha" type="text" class="commentBoxforms" id="captcha" /></td>
            </tr>
            <tr>
              <td> </td>
              <td><input type="submit" class="Button1" value="Send Message" id="submit" name="submit"/>
                  <input type="reset" class="Button1" value="Reset Form" /></td>
            </tr>
          </table>
          <a href="places_you_can_find_me.php"></a>
         </form>
         <?php
	 }
	 ?>

 

The problem I have here is that I can't get $random_string to = the user input. Help please?

Link to comment
https://forums.phpfreaks.com/topic/228823-captcha-help/#findComment-1179676
Share on other sites

Yup.  Looks good to me.  I would just create a function to create a random string and then go from there.  Here is an article on how to create that exact function..

 

http://snippets.dzone.com/posts/show/3123

 

I have it working so that it generates a random string, but I can't get that string to echo in the html so the user knows what to type.

Link to comment
https://forums.phpfreaks.com/topic/228823-captcha-help/#findComment-1179681
Share on other sites

I know you want to do it yourself, but I have a simple captcha that does as you said.

 

File 1 contains the list of characters to use and those to avoid....

<?php
  //returns a string of random text
  function random_text($count, $rm_similar = true){
  
    //create list of characters
    $chars = array_flip(array_merge(range(0,9), range('A','Z'), range('a','z')));
    
    //remove similar charactes that might lead to confussion
    if ($rm_similar){
    
      unset($chars[0],
            $chars[1],
            //Ad characters that could be confused 1/l; 2/Z; O/0; 8/B.....
            $chars['Z']);
    } //end if
     
    //generate the string of random text
    for ($i = 0, $text = ''; $i < $count; $i++){
      $text .= array_rand($chars);
    } //end for
  return $text;
   
  } //end function
?>

 

Then you have another script that creates an image;

<?php
  //This point to the file above
  include 'exceptions.php';

// must start or continue session and save CAPTCHA string in $_SESSION for it
// to be available to other requests
if (!isset($_SESSION))
{
    session_start();
    header('Cache-control: private');
}

// create an image
$width = 155;
$height = 20;
$image = imagecreate(170, 20);

// fill the image background color
$bg_color = imagecolorallocate($image, 0x33, 0x66, 0xFF);
imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);

// fetch random text
$text = random_text(5); //5 is the number of characters, increase-decrease as needed 

// determine x and y coordinates for centering text
$font = 8;
$x = imagesx($image) / 2 - strlen($textp) * imagefontwidth($font) / 2;
$y = imagesy($image) / 2 - imagefontheight($font) / 2;

// write text on image
$fg_color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
imagestring($image, $font, $x, $y, $text, $fg_color);

// save the CAPTCHA string for later comparison
$_SESSION['captcha'] = $text1

// output the image
header('Content-type: image/png');
imagepng($image);

imagedestroy($image);
?>

 

This is how you call the captcha from the HTML website;

<img src="../images/captcha.php?nocache=<?php echo time(); ?>" alt=""/>
<input type="text" name="captcha" size="1/>

 

 

An finally how you can process the information sent from the form;

if(empty($_POST["captcha"])){
      echo "Please enter a CAPTCHA security code.<br />";
    }//if(empty($_POST["captcha"]))
    elseif($_SESSION["captcha"] !== $captcha){
        $msg_reg .= "Please enter the correct CAPTCHA security code.<br />";
    }//elseif($_SESSION["captcha"] !== $captcha)

 

I hope this helps you on your journey... you can see mine (with modifications) working on http://merso.eu click on registration link.

 

Regards

Link to comment
https://forums.phpfreaks.com/topic/228823-captcha-help/#findComment-1179690
Share on other sites

I know you want to do it yourself, but I have a simple captcha that does as you said.

 

File 1 contains the list of characters to use and those to avoid....

<?php
  //returns a string of random text
  function random_text($count, $rm_similar = true){
  
    //create list of characters
    $chars = array_flip(array_merge(range(0,9), range('A','Z'), range('a','z')));
    
    //remove similar charactes that might lead to confussion
    if ($rm_similar){
    
      unset($chars[0],
            $chars[1],
            //Ad characters that could be confused 1/l; 2/Z; O/0; 8/B.....
            $chars['Z']);
    } //end if
     
    //generate the string of random text
    for ($i = 0, $text = ''; $i < $count; $i++){
      $text .= array_rand($chars);
    } //end for
  return $text;
   
  } //end function
?>

 

Then you have another script that creates an image;

<?php
  //This point to the file above
  include 'exceptions.php';

// must start or continue session and save CAPTCHA string in $_SESSION for it
// to be available to other requests
if (!isset($_SESSION))
{
    session_start();
    header('Cache-control: private');
}

// create an image
$width = 155;
$height = 20;
$image = imagecreate(170, 20);

// fill the image background color
$bg_color = imagecolorallocate($image, 0x33, 0x66, 0xFF);
imagefilledrectangle($image, 0, 0, $width, $height, $bg_color);

// fetch random text
$text = random_text(5); //5 is the number of characters, increase-decrease as needed 

// determine x and y coordinates for centering text
$font = 8;
$x = imagesx($image) / 2 - strlen($textp) * imagefontwidth($font) / 2;
$y = imagesy($image) / 2 - imagefontheight($font) / 2;

// write text on image
$fg_color = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
imagestring($image, $font, $x, $y, $text, $fg_color);

// save the CAPTCHA string for later comparison
$_SESSION['captcha'] = $text1

// output the image
header('Content-type: image/png');
imagepng($image);

imagedestroy($image);
?>

 

This is how you call the captcha from the HTML website;

<img src="../images/captcha.php?nocache=<?php echo time(); ?>" alt=""/>
<input type="text" name="captcha" size="1/>

 

 

An finally how you can process the information sent from the form;

if(empty($_POST["captcha"])){
      echo "Please enter a CAPTCHA security code.<br />";
    }//if(empty($_POST["captcha"]))
    elseif($_SESSION["captcha"] !== $captcha){
        $msg_reg .= "Please enter the correct CAPTCHA security code.<br />";
    }//elseif($_SESSION["captcha"] !== $captcha)

 

I hope this helps you on your journey... you can see mine (with modifications) working on http://merso.eu click on registration link.

 

Regards

 

Finally, an image generated captcha script that doesn't require downloading a ton of mb's worth of files. I'll try it out real quick and let you know if I encounter any problems...thanks for the script!

Link to comment
https://forums.phpfreaks.com/topic/228823-captcha-help/#findComment-1179707
Share on other sites

Couple things...

 

I stored both captcha scripts in a directory called "captcha", called the image generating script "captcha.php", and called the captcha in my html as so:

<img src="../captcha/captcha.php?nocache=<?php echo time(); ?>" alt=""/>

 

Is this right? Because the captcha is not being displayed on my website.

Link to comment
https://forums.phpfreaks.com/topic/228823-captcha-help/#findComment-1179716
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.