19/06/2012, 17:18
|
| | Fecha de Ingreso: enero-2010
Mensajes: 20
Antigüedad: 14 años, 3 meses Puntos: 0 | |
Respuesta: Problema con formulario con captcha "Could not open socket" Continuación.. Código PHP: /** * gets a URL where the user can sign up for reCAPTCHA. If your application * has a configuration page where you enter a key, you should provide a link * using this function. ??????????????????????????????????????????? * @param string $domain The domain where the page is hosted * @param string $appname The name of your application */ function recaptcha_get_signup_url ($domain = null, $appname = null) { return "http://recaptcha.net/api/getkey?" . _recaptcha_qsencode (array ('domain' => $domain, 'app' => $appname)); } function _recaptcha_aes_pad($val) { $block_size = 16; $numpad = $block_size - (strlen ($val) % $block_size); return str_pad($val, strlen ($val) + $numpad, chr($numpad)); } /* Mailhide related code */ function _recaptcha_aes_encrypt($val,$ky) { if (! function_exists ("mcrypt_encrypt")) { die ("To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed."); } $mode=MCRYPT_MODE_CBC; $enc=MCRYPT_RIJNDAEL_128; $val=_recaptcha_aes_pad($val); return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); } function _recaptcha_mailhide_urlbase64 ($x) { return strtr(base64_encode ($x), '+/', '-_'); } /* gets the reCAPTCHA Mailhide url for a given email, public key and private key */ function recaptcha_mailhide_url($pubkey, $privkey, $email) { if ($pubkey == '' || $pubkey == null || $privkey == "" || $privkey == null) { die ("To use reCAPTCHA Mailhide, you have to sign up for a public and private key, " . "you can do so at <a href='http://mailhide.recaptcha.net/apikey'>http://mailhide.recaptcha.net/apikey</a>"); } $ky = pack('H*', $privkey); $cryptmail = _recaptcha_aes_encrypt ($email, $ky); return "http://mailhide.recaptcha.net/d?k=" . $pubkey . "&c=" . _recaptcha_mailhide_urlbase64 ($cryptmail); } /** * A ReCaptchaResponse is returned from recaptcha_check_answer() */ class ReCaptchaResponse { var $is_valid; var $error; } /** * Calls an HTTP POST function to verify if the user's guess was correct * @param string $privkey * @param string $remoteip * @param string $challenge * @param string $response * @param array $extra_params an array of extra variables to post to the server * @return ReCaptchaResponse */ function recaptcha_check_answer ($privkey, $remoteip, $challenge, $response, $extra_params = array()) { if ($privkey == null || $privkey == '') { 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 ($remoteip == null || $remoteip == '') { die ("For security reasons, you must pass the remote ip to reCAPTCHA"); } //discard spam submissions if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) { $recaptcha_response = new ReCaptchaResponse(); $recaptcha_response->is_valid = false; $recaptcha_response->error = 'incorrect-captcha-sol'; return $recaptcha_response; } $response = _recaptcha_http_post (RECAPTCHA_VERIFY_SERVER, "/verify", array ( 'privatekey' => $privkey, 'remoteip' => $remoteip, 'challenge' => $challenge, 'response' => $response ) + $extra_params ); $answers = explode ("\n", $response [1]); $recaptcha_response = new ReCaptchaResponse(); if (trim ($answers [0]) == 'true') { $recaptcha_response->is_valid = true; } else { $recaptcha_response->is_valid = false; $recaptcha_response->error = $answers [1]; } // TODO: enlever cela pour la version de prod // $recaptcha_response = new ReCaptchaResponse(); // $recaptcha_response->is_valid = true; return $recaptcha_response; } /** * gets the parts of the email to expose to the user. * eg, given johndoe@example,com return ["john", "example.com"]. * the email is then displayed as [email protected] */ function _recaptcha_mailhide_email_parts ($email) { $arr = preg_split("/@/", $email ); if (strlen ($arr[0]) <= 4) { $arr[0] = substr ($arr[0], 0, 1); } else if (strlen ($arr[0]) <= 6) { $arr[0] = substr ($arr[0], 0, 3); } else { $arr[0] = substr ($arr[0], 0, 4); } return $arr; } /** * Gets html to display an email address given a public an private key. * to get a key, go to: * * http://mailhide.recaptcha.net/apikey */ function recaptcha_mailhide_html($pubkey, $privkey, $email) { $emailparts = _recaptcha_mailhide_email_parts ($email); $url = recaptcha_mailhide_url ($pubkey, $privkey, $email); return htmlentities($emailparts[0]) . "<a href='" . htmlentities ($url) . "' onclick=\"window.open('" . htmlentities ($url) . "', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;\" title=\"Reveal this e-mail address\">...</a>@" . htmlentities ($emailparts [1]); } ?> registrer.php Código PHP: <?php include('header.php'); /* require_once('recaptchalib.php'); $errors = ""; if (isset($_POST['register'])){ $privatekey = "6Lc1ygYAAAAAAJlhy7VhAVC_0CMai3NI1kiWaKPJ "; $resp = recaptcha_check_answer ($privatekey, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"], array("t" => md5(time()))); $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`) VALUES ('$username', '$password', '$email', '$ip')"); } } if($errors != "") { $maincontent = $errors; } else { $maincontent = "<strong>You have registered successfully!</strong> You may now log in."; } } $publickey = "6Lc1ygYAAAAAAKsxz0u-YJDGqZtI5stPq3Zy4e24"; */ if(!isset($_POST['register'])){ $maincontent = get_page('ajaxRegister'); $maincontent = str_replace('[[compty]]', $compty, $maincontent); } $layout = str_replace('[[main-content]]', $maincontent, $layout); echo $layout; ?> Muchas gracias! |