Jump to content

Recommended Posts

Hi I've tried adding a recaptcha for my register page.  For some reason it doesn't register , it also does't do recap check properly here is the code:  any ideas?

 

 

<html>

<head>

<link rel="stylesheet" type="text/css" href="./css/page.css" />

<br>Be sure to use only lowercase letters for account for account.<br />

</head>

 

<body>

<table

 

<?php

                    # Get the reCAPTCHA library

                    require_once('recaptchalib.php');

 

                    # These are /not/ real keys - you must replace them with your *own* keys

                    # obtained from http://recaptcha.net/api/getkey

                    define('PUBLIC_KEY',  'recap public key');

                    define('PRIVATE_KEY', 'recap private key');

 

                    # Did the user fail the captcha test?

                    $error = null;

require('./configs/reg_conf.php');

function doesUsernameExist($name){

    $exit = FALSE;

    $result = @mssql_query("SELECT * FROM ACCOUNT_TBL WHERE account='$name'");

    if (mssql_num_rows($result) != 0){

        $exit = TRUE;

    }

    return $exit;

}

 

if(isset($_POST['submit'])){

    $user = preg_replace ("[^A-Za-z0-9]", "", $_POST['username']);

    $pass = preg_replace ("[^A-Za-z0-9]", "", $_POST['password']);

    if($_POST['username'] == ""){

        echo '<font color="red">Enter a user.</font><br /><br />';

    }

    else if($_POST['password'] == ""){

echo '<font color="red">Enter a password.</font><br /><br />';

    }

    else if ((strlen($_POST['username']) > 16) || (strlen($_POST['username']) < 3)){

echo '<font color="red">The user should be 3 to 16 characters.</font><br /><br />';

    }

    else if ((strlen($_POST['password']) > 16) || (strlen($_POST['password']) < 3)){

echo '<font color="red">The password should be 3 to 16 characters.</font><br /><br />';

    }

    else if($_POST['username'] != $user){

echo '<font color="red">User with invalid characters.</font><br /><br />';

    }

    else if($_POST['password'] != $pass){

echo '<font color="red">Password with invalid characters.</font><br /><br />';

    }

    else {

$pass = md5('kikugalanet' . $pass);

                if ($_POST["recaptcha_response_field"]) {

                        $response = recaptcha_check_answer(

                            PRIVATE_KEY, $_SERVER['REMOTE_ADDR'],

                            $_POST['recaptcha_challenge_field'],

                            $_POST['recaptcha_response_field']

                        );

 

                        if ( $response->is_valid ) {

                            # The user passed the reCAPTCHA test: form submission should continue

                            # Your other form validation logic should go here.

                       

                            # For example

                            # ... validate user input ...

                            # ... store form data in a database ...

                            # ... redirect to 'thank you' page

                       

                        }

                        else {

                            # The user failed the reCAPTCHA test so we need

                            # to fill in the error message and re-try the

                            # form submission

                            $error = $response->error;

                        }

                    }

            if(!doesUsernameExist($user)){

                $stmt = mssql_init('createaccount', $link);

                mssql_bind($stmt, '@account', $user, SQLVARCHAR, false, false, 15);

                mssql_bind($stmt, '@password', $pass, SQLVARCHAR, false, false, 32);

 

 

                mssql_execute($stmt) or die ("Something is wrong on the execution");

                mssql_free_statement($stmt);

echo '<font color="blue">Register Successful.</font><br /><br />';

            }

            else {

echo '<font color="red">User already Exist.</font><br /><br />';

            }

        }

mssql_close();

    }

 

echo '<form action="register.php" method="post">';

echo  recaptcha_get_html( PUBLIC_KEY, $error );

echo 'Username: <input type="text" name="username" /><br />';

echo 'Password: <input type="password" name="password" /><br />';

echo '<input type="submit" name="submit" value="Register" />';

echo '</form>';

 

?>

</body>

</html>

 

Also heres a test code for just recapture on its own which works:

 

    <html>

        <body>

            <form action="" method="post">

                <?php

                    # Get the reCAPTCHA library

                    require_once('recaptchalib.php');

 

                    # These are /not/ real keys - you must replace them with your *own* keys

                    # obtained from http://recaptcha.net/api/getkey

                    define('PUBLIC_KEY',  'public');

                    define('PRIVATE_KEY', 'private');

 

                    # Did the user fail the captcha test?

                    $error = null;

 

                    # This is where we process the user's response. We don't

                    # do this when the form is initially displayed - only

                    # when the user submits it.

                    if ($_POST["recaptcha_response_field"]) {

                        $response = recaptcha_check_answer(

                            PRIVATE_KEY, $_SERVER['REMOTE_ADDR'],

                            $_POST['recaptcha_challenge_field'],

                            $_POST['recaptcha_response_field']

                        );

 

                        if ( $response->is_valid ) {

                            # The user passed the reCAPTCHA test: form submission should continue

                            # Your other form validation logic should go here.

                       

                            # For example

                            # ... validate user input ...

                            # ... store form data in a database ...

                            # ... redirect to 'thank you' page

                       

                        }

                        else {

                            # The user failed the reCAPTCHA test so we need

                            # to fill in the error message and re-try the

                            # form submission

                            $error = $response->error;

                        }

                    }

               

                    # Display the reCAPTCHA challenge. The first time

                    # through $error will be null.

 

                    echo recaptcha_get_html( PUBLIC_KEY, $error );

                ?>

                <br />

                <!-- example form fields - your own fields go here -->

                Username: <input type="text" name="username" value="" /><br />

                Email address: <input type="text" name="email"    value="" /><br />

                <input type="submit" value="submit" />

            </form>

        </body>

    </html>

 

Link to comment
https://forums.phpfreaks.com/topic/201244-my-register-page-script-recaptcha/
Share on other sites

Here's mine... Hopefully you can take the bits you need:

 

<?php
include "recaptcha.php";

if (isset($_POST['submit'])) {
	$username = trim($_POST['username']); $password = trim($_POST['password1']); $password2 = trim($_POST['password2']); $email = trim($_POST['email']);
	$captcha = recaptcha_check_answer ($privatekey, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);

	if ($password == NULL || $password2 == NULL || $email == NULL || $username == NULL)
		$error = "Please ensure that every field is complete.";
	else if (!$captcha->is_valid)
		$error = "The Captcha you entered was invalid. If you cannot read the text please click the refresh button. If you feel there's a problem with the Captcha system please <a href=\"mailto:[email protected]\">e-mail us</a> on: <a href=\"mailto:[email protected]\">[email protected]</a>";
	else if (!preg_match("/^[a-zA-Z0-9 ]+$/", $username))
		$error = "Your username can only contain numbers, letters and spaces.";
	else if (!preg_match("/^[a-zA-Z0-9 ]+$/", $password))
		$error = "Your password can only contain numbers, letters and spaces.";
	else if ($password != $password2)
		$error = "Your passwords did not match. Please ensure they're the same.";
	else if (strlen($username) > 25 || strlen($username) < 2)
		$error = "Your username must be between 2 and 25 characters in length.";
	else if (strlen($password) > 16 || strlen($password) < 4)
		$error = "Your password must be between 4 and 16 characters in length.";
	else if (strlen($email) > 70 || strlen($email) < 10)
		$error = "Your e-mail must be between 10 and 70 characters in length.";
	else if (!preg_match("/^\b[A-Z0-9._-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$\b/i", $email))
		$error = "Your e-mail seems to be invalid.";
	else {
?>

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.