Jump to content

Implement Captcha into form?


Graxeon

Recommended Posts

Hello! I'm trying to put captcha into my code but I can't seem to get it right.

 

Note: I removed the captcha code from my form because I keep doing it incorrectly and don't want the form to seem cluttered :P. I need someone to show me how to implement the captcha code into my form.

 

form.html

<!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>JW FLV Player</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>

</head>
<body id="main_body" >

<img id="top" src="top.png" alt="">
<div id="form_container">

	<form id="form_229096" class="appnitro"  method="get" action="urldisplay.php">
				<div class="form_description">
		<h2>Have a video URL? Fill this out.</h2>
	</div>						
		<ul >

				<li id="li_2" >
	<label class="description" for="element_2">Who made this video? </label>
	<div>
	<select class="element select medium" id="element_2" name="element_2"> 
		<option value="" selected="selected"></option>
<option value="User1" >User1</option>
<option value="User2" >User2</option>
<option value="User3" >User3</option>

	</select>
	</div> 
	</li>		<li id="li_3" >
	<label class="description" for="element_3">Which episode is it? </label>
	<div>
	<select class="element select medium" id="element_3" name="element_3"> 
		<option value="" selected="selected"></option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>

	</select>
	</div> 
	</li>		<li id="li_4" >
	<label class="description" for="element_4">Where did you find this URL? </label>
	<div>
	<select class="element select medium" id="element_4" name="element_4"> 
		<option value="" selected="selected"></option>
<option value="YouTube" >YouTube</option>
<option value="Direct" >Direct FLV/MP4</option>

	</select>
	</div> 
	</li>		<li id="li_1" >
	<label class="description" for="element_1">URL </label>
	<div>
		<input id="element_1" name="element_1" class="element text large" type="text" maxlength="255" value=""/> 
	</div> 
	</li>
   

				<li class="buttons">
		    <input type="hidden" name="form_id" value="229096" />
		    
			<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
	</li>
		</ul>
	</form>	
	<div id="footer">
		Copyright
	</div>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
</html>

 

urldisplay.php

<?php

$xml = '';
$xml .= '<font size=10>Now Playing...</font>' . PHP_EOL;
$xml .= '<br />' . $_GET['element_2'] . ', Episode ' . $_GET['element_3'] . PHP_EOL;
$xml .= '<br />Host:' . $_GET['element_4'] . PHP_EOL;
$xml .= "<br><br>" . PHP_EOL;

$xml .= "<script type='text/javascript' src='mediaplayer/swfobject.js'></script>" . PHP_EOL;
$xml .= "<div id='mediaspace'>This text will be replaced</div>" . PHP_EOL;
$xml .= "<script type='text/javascript'>" . PHP_EOL;
$xml .= "var so = new SWFObject('mediaplayer/player.swf','ply','470','320','9','#ffffff');" . PHP_EOL;
$xml .= "so.addParam('allowfullscreen','true');" . PHP_EOL;
$xml .= "so.addParam('allowscriptaccess','always');" . PHP_EOL;
$xml .= "so.addParam('wmode','opaque');" . PHP_EOL;
$xml .= "so.addVariable('file','" . $_GET['element_1'] . "');" . PHP_EOL;
$xml .= "so.write('mediaspace');" . PHP_EOL;
$xml .= "</script>" . PHP_EOL;


  $xml .= "<br><br>" . PHP_EOL;
  $xml .= "<br>" . PHP_EOL;
  $xml .= '<a href="javascript:history.back()"><---back to the form</a>' . PHP_EOL;
  $xml .= "<br>" . PHP_EOL;
  
  $mainemail = "someemail@somsite.com";
  $subject = "Video Submit Check";
  $subemail = "whoknows@somesite.com";
  $from = "From: $subemail";
  $ip=$_SERVER['REMOTE_ADDR'];
  $content = $_GET['element_2'] . " _____" . $_GET['element_3'] . " _____" . $_GET['element_4'] . " _____" . $_GET['element_1'] . " _____" . $ip;
  $send = mail($mainemail, $subject, $content, $from) ;

print $xml;

?>

 

 

So as they are...form.html and urldisplay.php are both working. Now I need to add some security to it through Captcha but I can't get it right. Help, please?

 

 

Here is the Captcha site + code: www.captcha.net

 

<html>
  <body>
    <form action="" method="post">
<?php

require_once('recaptchalib.php');

// Get a key from http://recaptcha.net/api/getkey
$publickey = "123456";
$privatekey = "654321";

# the response from reCAPTCHA
$resp = null;
# the error code from reCAPTCHA, if any
$error = null;

# was there a reCAPTCHA response?
if ($_POST["recaptcha_response_field"]) {
        $resp = recaptcha_check_answer ($privatekey,
                                        $_SERVER["REMOTE_ADDR"],
                                        $_POST["recaptcha_challenge_field"],
                                        $_POST["recaptcha_response_field"]);

        if ($resp->is_valid) {
                echo "You got it!";
        } else {
                # set the error code so that we can display it
                $error = $resp->error;
        }
}
echo recaptcha_get_html($publickey, $error);
?>
    <br/>
    <input type="submit" value="submit" />
    </form>
  </body>
</html>

Link to comment
Share on other sites

No you explained it ok, i think it just looks like a big question lol, anyway;

 

This goes in the form (wherever you want it to show)

<?php

require_once('recaptchalib.php');
$publickey = "...";
echo recaptcha_get_html($publickey);
?>

(The form MUST be able to process php code, usually this means it must have a .php extension).

For the processing form(validation);

require_once('recaptchalib.php');
$privatekey = "...";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

// This part can be modified to fit the style of your website, etc.
if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}

 

It tells you here:

http://recaptcha.net/plugins/php/

 

-cb-

Link to comment
Share on other sites

Lol thanks :P

 

I get an error after putting your first code into the form.html file (well, form.php now) and your second code into urldisplay.php like so:

 

<?php

require_once('recaptchalib.php');
$privatekey = "my key";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

// This part can be modified to fit the style of your website, etc.
if (!$resp->is_valid) {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
}

$xml = '';
$xml .= '<font size=10>Now Playing...</font>' . PHP_EOL;
$xml .= '<br />' . $_GET['element_2'] . ', Episode ' . $_GET['element_3'] . PHP_EOL;
$xml .= '<br />Host:' . $_GET['element_4'] . PHP_EOL;
$xml .= "<br><br>" . PHP_EOL;

//and the rest of the code...

 

Should my "print $xml" code be within the "if" statement or am I doing something else wrong?

 

Here's the error:

 

The reCAPTCHA wasn't entered correctly. Go back and try it again.(reCAPTCHA said: incorrect-captcha-sol)

 

It was entered correctly, though. And recaptchalib.php is in the same directory.

Link to comment
Share on other sites

Yeah I put my form processor within the if statement. Like this:

 

  if ($resp->is_valid) {
//my code here
  } else {
    # set the error code so that we can display it. You could also use
    # die ("reCAPTCHA failed"), but using the error message is
    # more user friendly
    $error = $resp->error;
  }
}

 

I don't get an error but the Captcha code isn't doing anything now. I click "submit" without filling out the Captcha box and it still lets me through.

Link to comment
Share on other sites

Oh yeah sorry this is what I have (forgot to replace when transferring to forums :P)

 

  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .

      "(reCAPTCHA said: " . $resp->error . ")");

 

And yes, the keys are correct.

Link to comment
Share on other sites

Changing it to POST messes up urldisplay.php because it doesn't receive the inputs. Well, it does for the Captcha code but not for processing the video because of the "$_GET[element_#"]" things.

 

Edit: and even with the POST method...captcha doesnt work. It lets me through without putting in the letters.

 

Edit2: print_r gave me the form inputs and some other random characters. I also changed the $_GETs to $_POSTs. The video display works, but the captcha isn't checking if the code was correct. I can still go through the form without typing in the security letters.

 

 

form.php

<!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>JW FLV Player</title>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>

</head>
<body id="main_body" >

<img id="top" src="top.png" alt="">
<div id="form_container">

	<form id="form_229096" class="appnitro"  method="get" action="urldisplay.php">
				<div class="form_description">
		<h2>Have a video URL? Fill this out.</h2>
	</div>						
		<ul >

				<li id="li_2" >
	<label class="description" for="element_2">Who made this video? </label>
	<div>
	<select class="element select medium" id="element_2" name="element_2"> 
		<option value="" selected="selected"></option>
<option value="User1" >User1</option>
<option value="User2" >User2</option>
<option value="User3" >User3</option>

	</select>
	</div> 
	</li>		<li id="li_3" >
	<label class="description" for="element_3">Which episode is it? </label>
	<div>
	<select class="element select medium" id="element_3" name="element_3"> 
		<option value="" selected="selected"></option>
<option value="1" >1</option>
<option value="2" >2</option>
<option value="3" >3</option>

	</select>
	</div> 
	</li>		<li id="li_4" >
	<label class="description" for="element_4">Where did you find this URL? </label>
	<div>
	<select class="element select medium" id="element_4" name="element_4"> 
		<option value="" selected="selected"></option>
<option value="YouTube" >YouTube</option>
<option value="Direct" >Direct FLV/MP4</option>

	</select>
	</div> 
	</li>		<li id="li_1" >
	<label class="description" for="element_1">URL </label>
	<div>
		<input id="element_1" name="element_1" class="element text large" type="text" maxlength="255" value=""/> 
	</div> 
	</li>
   

				<li class="buttons">
<?php

require_once('recaptchalib.php');
$publickey = "MYKEYHERE";
echo recaptcha_get_html($publickey);
?>
		    <input type="hidden" name="form_id" value="229096" />
		    
			<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
	</li>
		</ul>
	</form>	
	<div id="footer">
		Copyright
	</div>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>
</html>

 

urldisplay.php

 

<?php
print_r($_POST);
require_once('recaptchalib.php');
$privatekey = "MY2NDKEYHERE";
$resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

// This part can be modified to fit the style of your website, etc.
if (!$resp->is_valid) {
$xml = '';
$xml .= '<font size=10>Now Playing...</font>' . PHP_EOL;
$xml .= '<br />' . $_GET['element_2'] . ', Episode ' . $_GET['element_3'] . PHP_EOL;
$xml .= '<br />Host:' . $_GET['element_4'] . PHP_EOL;
$xml .= "<br><br>" . PHP_EOL;

$xml .= "<script type='text/javascript' src='mediaplayer/swfobject.js'></script>" . PHP_EOL;
$xml .= "<div id='mediaspace'>This text will be replaced</div>" . PHP_EOL;
$xml .= "<script type='text/javascript'>" . PHP_EOL;
$xml .= "var so = new SWFObject('mediaplayer/player.swf','ply','470','320','9','#ffffff');" . PHP_EOL;
$xml .= "so.addParam('allowfullscreen','true');" . PHP_EOL;
$xml .= "so.addParam('allowscriptaccess','always');" . PHP_EOL;
$xml .= "so.addParam('wmode','opaque');" . PHP_EOL;
$xml .= "so.addVariable('file','" . $_GET['element_1'] . "');" . PHP_EOL;
$xml .= "so.write('mediaspace');" . PHP_EOL;
$xml .= "</script>" . PHP_EOL;


  $xml .= "<br><br>" . PHP_EOL;
  $xml .= "If you would like to contribute to the website, please submit more video URLs through the same form." . PHP_EOL;
  $xml .= "<br>" . PHP_EOL;
  $xml .= '<a href="javascript:history.back()"><---back to the form</a>' . PHP_EOL;
  $xml .= "<br>" . PHP_EOL;
  $xml .= "NOTE: Video URLs have to be the FULL episode. So, videos that are split into 'parts' like those on YouTube can't be added to the website." . PHP_EOL;
  
  $mainemail = "grafeon@gmail.com";
  $subject = "Video Submit";
  $subemail = "whoknows@somesite.com";
  $from = "From: $subemail";
  $ip=$_SERVER['REMOTE_ADDR'];
  $content = $_GET['element_2'] . " _____" . $_GET['element_3'] . " _____" . $_GET['element_4'] . " _____" . $_GET['element_1'] . " _____" . $ip;
  $send = mail($mainemail, $subject, $content, $from) ;

print $xml;
  } else {
  die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
       "(reCAPTCHA said: " . $resp->error . ")");
     }

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.