Solved

Need a small php captcha

Posted on 2013-10-30
8
755 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
  • 4
  • 4
8 Comments
 
LVL 109

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 109

Expert Comment

by:Ray Paseur
ID: 39611499
Of course!
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

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 109

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 109

Expert Comment

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

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

831 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