Hos pego los códigos implicados:
Ajaxsaveregistration.php
Código PHP:
<?php
header("Content-Type: text/xml;charset=UTF-8");
include('header.php');
require_once('recaptchalib.php');
$errors = "";
$privatekey = "6LeC9dISAAAAAM44z-dpxanQvPJWDn-Vx4GgLd6";
try {
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"],
array("t" => md5(time())));
} catch(Exception $ex) {
$errors .= $ex->getMessage() . '<br />';
}
$username = $_POST['username'];
$password = $_POST['password'];
$password2 = $_POST['password2'];
$email = $_POST['email'];
$ip = $_SERVER['REMOTE_ADDR'];
if (!$resp->is_valid) {
$errors .= 'The captcha code you entered was not correct.<br/>';
}
if (empty($username) || empty($password) || empty($password2) || empty($email)){
$errors .= 'Please enter something in all of the fields.<br/>';
}
if (strlen($username) < 3 || strlen($username) > 20){
$errors .= 'Please keep your username between 3 and 20 letters.<br/>';
}
if ($password != $password2){
$errors .= 'The passwords you entered do not match.<br/>';
}
if (!preg_match("/^[a-z][a-z0-9_.-]+@[a-z0-9][a-z0-9-]+\.[a-z]+(\.[a-z]+)*$/i", $email)){
$errors .= 'The e-mail address you entered is not valid.<br/>';
}
if (strlen($email) > 40){
$errors .= 'Please keep your e-mail address lower than 40 letters.<br/>';
}
$check_username = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username'");
$check_email = mysql_query("SELECT `id` FROM `users` WHERE `email`='$email'");
if (mysql_num_rows($check_username) != 0) {
$errors .= 'That username is already taken by another user.<br/>';
}
if (mysql_num_rows($check_email) != 0) {
$errors .= 'We only allow one account per user.<br />';
} else {
if($errors == "") {
$password = md5(md5(sha1($password)));
$time = time();
mysql_query("INSERT INTO `users` (`username`,`password`,`email`,`ip`,`reg_date`) VALUES ('$username', '$password', '$email', '$ip', '" . date('Y-m-d H:i:s',time()) . "')");
}
}
if($errors != "") {
$maincontent = $errors;
} else {
$maincontent = "<strong>You have registered successfully!</strong> You may now log in.";
}
$publickey = "6LeC9dISAAAAALgCYFVWW8ZLPIw6WGN34BGf5COQ";
?>
<code><?php
/* Donne
Array
(
[username] => khalid
[password] => khalidpwd
[password2] => khalidpwd
[email] => [email protected]
[recaptcha_challenge_field] => 02J5iH75gdOFfyKNrvFr-Hz9MPxo4nrpoRWu_x-wP6v0oPK8rLZzMvd5cPtKpMcu8
jZcMJxryo3TNW9bTtHghAu5PAwYtbJQQovzPM8t3GMOmih2Qan3iD8Mdou_pwDgj2H8B9sl8FI-MFJUPEHZeG3RvYodxGw1uPxh3
yls9H4qOrYgg8lnuOB5mm4mO9L03vrB3FQLMmlxorK29RAm2aIWQ7rXLZMeZ0LFR-_QngSNQwRuj3m0NCUpGUXOXjHiDoUsfzQ0K
6a7K7UMq3jqakkC9-MlUB
[recaptcha_response_field] => peron smock
[PHPSESSID] => e5ackrv75hsi4kagssgi33ehm5
)
*/
if($errors != "") {
$result = "Errors occured:<br/>" . $errors;
} else {
$result = "Ok";
}
echo $result;
?></code>
recaptchalib.php
Código PHP:
<?php
/*
* This is a PHP library that handles calling reCAPTCHA.
* - Documentation and latest version
* http://recaptcha.net/plugins/php/
* - Get a reCAPTCHA API Key
* http://recaptcha.net/api/getkey
* - Discussion group
* http://groups.google.com/group/recaptcha
*
*/
/**
* The reCAPTCHA server URL's
*/
define("RECAPTCHA_API_SERVER", "http://api.recaptcha.net");
define("RECAPTCHA_API_SECURE_SERVER", "https://api-secure.recaptcha.net");
define("RECAPTCHA_VERIFY_SERVER", "api-verify.recaptcha.net");
/**
* Encodes the given data into a query string format
* @param $data - array of string elements to be encoded
* @return string - encoded request
*/
function _recaptcha_qsencode ($data) {
$req = "";
foreach ( $data as $key => $value )
$req .= $key . '=' . urlencode( stripslashes($value) ) . '&';
// Cut the last '&'
$req=substr($req,0,strlen($req)-1);
return $req;
}
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $host
* @param string $path
* @param array $data
* @param int port
* @return array response
*/
function _recaptcha_http_post($host, $path, $data, $port = 80) {
$req = _recaptcha_qsencode ($data);
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
$http_request .= "\r\n";
$http_request .= $req;
$response = '';
/** Use proxy here. **/
// include "RemoteFopenViaProxy.php";
// $obj = new RemoteFopenViaProxy("http://" . $host . "$path?$req", "svrbtproxy01", 8080);
//
// $obj->request_via_proxy();
// $response = $obj->get_result();
// echo $response;
if( false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
throw new Exception ('Could not open socket');
exit();
}
fwrite($fs, $http_request);
while ( !feof($fs) )
$response .= fgets($fs, 1160); // One TCP-IP packet
fclose($fs);
$response = explode("\r\n\r\n", $response, 2);
return $response;
}
/**
* Gets the challenge HTML (javascript and non-javascript version).
* This is called from the browser, and the resulting reCAPTCHA HTML widget
* is embedded within the HTML form it was called from.
* @param string $pubkey A public key for reCAPTCHA
* @param string $error The error given by reCAPTCHA (optional, default is null)
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @return string - The HTML to be embedded in the user's form.
*/
function recaptcha_get_html ($pubkey, $error = null, $use_ssl = false)
{
if ($pubkey == null || $pubkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>");
}
if ($use_ssl) {
$server = RECAPTCHA_API_SECURE_SERVER;
} else {
$server = RECAPTCHA_API_SERVER;
}
$errorpart = "";
if ($error) {
$errorpart = "&error=" . $error;
}
return '<script type="text/javascript" src="'. $server . '/challenge?k=' . $pubkey . $errorpart . '"></script>
<noscript>
<iframe src="'. $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/>
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
</noscript>';
}
Muchas gracias de antemano!