Solved

Need a small php captcha

Posted on 2013-10-30
8
762 Views
Last Modified: 2013-11-01
Hello,
I need help to resize (but keep readable) recaptcha down to 200px width or another captcha that is small and uses PHP and/or javascript.

Would prefer it to be numbers if possible.

I have implemented recaptcha successfully but it is too large to be in our sidebar where the form is.

I have used the code:
		#recaptcha_image img 
{ 
        width: 150px; 
} 	

Open in new window


Which works great for resizing the image but the rest of the recaptcha is still too big for my working area.

The testpage I am working on is:

http://visailing.com/Shopping-Cart/Captcha-Test.php

The captcha shouldn't be bigger then the email text box so it can fit onto the sidebar when called.

Thanks in advance for your direction.

Here is the complete code of my test form.

<?php
	define( API_PUBLIC_KEY, '6Lc9sOESAAAAAPeBaO8hi48A8CEc60B9dzhGJ8Sp' );
	define( API_PRIVATE_KEY, '6Lc9sOESAAAAAILGaphhS7fh-dxEUTh7kfbLMY7Z'  );
	require_once('recaptchalib.php');	
	$validated = false;
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Captch Test</title>
    <style type="text/css">
	#recaptcha_error_box {
        	width:30%;
			border:medium groove #A00;
            background-color:#FF9;
			padding:1em;
			color:#900;
			font-weight:bold;
			font-size:80%;
        }
		.table_success {
			background-color:#009;
			border:thin dashed #999;
			padding:2em;
			font-size:90%;
			color:#FFF;
		}	
		#recaptcha_image img { 
        width: 150px; 
		} 	
    </style>
    	<script>
    var RecaptchaOptions = {
       theme : 'blackglass',
       tabindex : 2
    };
    </script>
</head>
<body>

<?
	if( $_POST['validate'] === 'yes' ) { 
		$response = recaptcha_check_answer( API_PRIVATE_KEY,
										   	$_SERVER['REMOTE_ADDR'],
											$_POST['recaptcha_challenge_field'],
											$_POST['recaptcha_response_field']
											);
		
		if( ! $response->is_valid ) {		
			echo '<div id="recaptcha_error_box">The reCAPTCHA failed with this message: '.$response->error.'<br />Please try again.</div>';
		}
		else {
			$validated = true;
			$emailbody = "The following yachts and information were submitted through the <b>My Yacht List</b>:<br>";
	$emailbody .= "<b><br>Email Address:  ".$email."</b>";
	if($clientname =="") {} else { $emailbody .= "<br>Client Name:  ". $clientname;}
	
	
			$date= date("m/d/y h:i:s");
			include_once('/var/www/visailing.com/_mail/class.phpmailer.php');
			$mail    = new PHPMailer();
			$body    = $emailbody;
			$mail->From     = "myyachtlist@visailing.com";
			$mail->FromName = "My Yacht List";
			$mail->Subject = "My Yacht List Request Availability:  ".$date ;
			$mail->MsgHTML($emailbody);
			$mail->AddAddress("heather@visailing.com");
			if(!$mail->Send()) { echo 'Failed to send mail';
			} else {  
			;}
			$requestmessage = "<br>&nbsp;<br><font color='red'>Your request has been sent</font><br>";
            header("Location: http://www.visailing.com/caribbean-sailing/yacht-availability-thank-you.php?requesttype=myyachtlist");
		}	
	 } 
?>
            
   <?php if( ! $validated ) { ?>         
 <form method="post">
<font color='007698' size='2'>
Full Name:<br>
<input type="text" id="clientname" name="clientname" ><br>
Email :<br>
<input type="text" id="email" name="email"><br>
<?php echo recaptcha_get_html(API_PUBLIC_KEY); ?>
<input type="hidden" name="validate" value="yes" />
<input type='submit' value='Submit' name='submit' align='right'>
</font>
</form>
<?php }  ?>
</body>
</html>

Open in new window

0
Comment
Question by:Razzmataz73
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39611484
0
 

Author Comment

by:Razzmataz73
ID: 39611494
Wow!
That looks amazing!
I am leaving now but will try one of those options later tonight.
Can I post to this thread if I have questions?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39611499
Of course!
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

Author Comment

by:Razzmataz73
ID: 39614016
This is GREAT!
I am going to go with your code for:
An Image-based CAPTCHA Test

I have it working but how do I make it so if they enter in the correct text it goes to my code:
//If Captcha is valid then do this

I have it running on this test page:
http://www.visailing.com/Shopping-Cart/Captcha-Test.php

And here is the code:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Captch Test</title>
</head>
<body>

<?
//If Captcha is valid then do this
	if( $_POST['validate'] === 'yes' ) { 
		if( ! $response->is_valid ) {		
			echo '<div id="recaptcha_error_box">The reCAPTCHA failed with this message: '.$response->error.'<br />Please try again.</div>';
		}
		else {
			$validated = true;
	$emailbody = "The following yachts and information were submitted through the <b>My Yacht List</b>:<br>";
	$emailbody .= "<b><br>Email Address:  ".$email."</b>";
	if($clientname =="") {} else { $emailbody .= "<br>Client Name:  ". $clientname;}
	
	
			$date= date("m/d/y h:i:s");
			include_once('/var/www/visailing.com/_mail/class.phpmailer.php');
			$mail    = new PHPMailer();
			$body    = $emailbody;
			$mail->From     = "myyachtlist@visailing.com";
			$mail->FromName = "My Yacht List";
			$mail->Subject = "My Yacht List Request Availability:  ".$date ;
			$mail->MsgHTML($emailbody);
			$mail->AddAddress("heather@visailing.com");
			if(!$mail->Send()) { echo 'Failed to send mail';
			} else {  
			;}
			$requestmessage = "<br>&nbsp;<br><font color='red'>Your request has been sent</font><br>";
            header("Location: http://www.visailing.com/caribbean-sailing/yacht-availability-thank-you.php?requesttype=myyachtlist");
		}	
	 } 
?>
   <?php if( ! $validated ) { ?>              
       
 <form method="post">
<font color='007698' size='2'>
Full Name:<br>
<input type="text" id="clientname" name="clientname" ><br>
Email :<br>
<input type="text" id="email" name="email"><br>

<?

error_reporting(E_ALL);

// FUNCTION TO MAKE A RANDOM STRING
function random_string($length=5)
{
    // POSSIBLE PERMUATIONS http://www.mathwords.com/p/permutation_formula.htm
    // pow($length,strlen($alpha)); = 32*31*30*29*28 = 24,165,120 IF LENGTH IS 5
    //        1...5...10...15...20...25...30......
    $alpha = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    $array = str_split($alpha);
    $randy = NULL;
    while(strlen($randy) < $length)
    {
        // CHOOSE A LETTER AT RANDOM, USE IT AND REMOVE IT
        $point  = mt_rand(0, count($array)-1);
        $randy .= $array[$point];
        unset($array[$point]);
        $array = array_values($array);
    }
    return $randy;
}

// TEST THE FUNCTION
echo "Please enter the following captcha:<br><b>".random_string();

?>
</b><br>
<input type="text" name="validate" id="validate" />
<br>
<input type='submit' value='Submit' name='submit' align='right'>
</font>
</form>
<?php }  ?>
</body>
</html>

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39614104
You would want to store the CAPTCHA code in the session array.  IIRC the article discusses that part.

See http://laprbass.com/RAY_temp_razzmataz73.php

<?php // RAY_temp_razzmataz.php
error_reporting(E_ALL);

// START THE SESSION ON EVERY PAGE, NO EXCEPTIONS
session_start();

// IF THERE IS A POST-METHOD REQUEST
if (!empty($_POST))
{
    if (strtoupper($_POST['captcha']) == strtoupper($_SESSION['captcha']))
    {
        echo 'YOU GOT IT RIGHT';
        /* RUN THE REST OF THE SCRIPT HERE */
    }
    else
    {
        echo 'YOU GOT IT WRONG';
        /* RUN THE ERROR HANDLER FOR CAPTCHA HERE */
    }
}

// SET THE CAPTCHA VALUE IN THE SESSION
$captcha = random_string();
$_SESSION['captcha'] = $captcha;

// CREATE THE FORM
$form = <<<EOD
<form method="post">
<font color='007698' size='2'>
Full Name:<br>
<input type="text" id="clientname" name="clientname" ><br>
Email :<br>
<input type="text" id="email" name="email"><br>
Please Enter $captcha<br>
<input type="text" name="captcha" id="validate" />
<br>
<input type='submit' value='Submit' name='submit' align='right'>
</font>
</form>
EOD;

echo $form;


// FUNCTION TO MAKE A RANDOM STRING
function random_string($length=5)
{
    // POSSIBLE PERMUATIONS http://www.mathwords.com/p/permutation_formula.htm
    // pow($length,strlen($alpha)); = 32*31*30*29*28 = 24,165,120 IF LENGTH IS 5
    //        1...5...10...15...20...25...30......
    $alpha = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
    $array = str_split($alpha);
    $randy = NULL;
    while(strlen($randy) < $length)
    {
        // CHOOSE A LETTER AT RANDOM, USE IT AND REMOVE IT
        $point  = mt_rand(0, count($array)-1);
        $randy .= $array[$point];
        unset($array[$point]);
        $array = array_values($array);
    }
    return $randy;
}

Open in new window

HTH, ~Ray
0
 

Author Comment

by:Razzmataz73
ID: 39616610
It worked PERFECTLY!!!

Thank YOU!!
0
 

Author Closing Comment

by:Razzmataz73
ID: 39616613
Worked exactly as he said it would!
Great Support!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39616622
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question