Solved

Need a small php captcha

Posted on 2013-10-30
8
748 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 108

Expert Comment

by:Ray Paseur
Comment Utility
0
 

Author Comment

by:Razzmataz73
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Of course!
0
 

Author Comment

by:Razzmataz73
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
It worked PERFECTLY!!!

Thank YOU!!
0
 

Author Closing Comment

by:Razzmataz73
Comment Utility
Worked exactly as he said it would!
Great Support!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE! ~Ray
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now