我的验证码没有进入我的注册表格

时间:2014-06-08 10:54:39

标签: javascript php jquery html mysql

我正在制作我的注册表格,如果用户键入正确的验证码,那么用户的数据将被存储到数据库中,如果验证码错误那么它会将输出打印为错误但我的问题是如果用户输入了错误验证码的代码,它将用户的数据存储到数据库中,并显示“错误的代码”输出,请帮助我错了?

这是我的代码。

<?php session_start(); ?>
<?php
// show potential errors / feedback (from registration object)
if (isset($registration)) {
    if ($registration->errors) {
        foreach ($registration->errors as $error) {
            echo '<div class="alert-box error"><span>Error: </span>'.$error.'</div>';
        }
    }
    if ($registration->messages) {
        foreach ($registration->messages as $message) {
            echo '<div class="alert-box success"><span>Success: </span>'.$message.'</div>';
        }
    }
}

/** Validate captcha */
if (!empty($_REQUEST['captcha'])) {
    if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha']) {
        $captcha_message ="Invalid captcha";
        $style = "background-color: #FF606C color:#555
        border-radius: 0px
        font-family:Tahoma,Geneva,Arial,sans-serif font-size:11px;
        font-size: 18px
        padding:  30px 36px
        margin:10px font-weight:bold
        text-transform:uppercase border:2px solid #0c0b0b
        background-color: #ff7e48 ";

        echo'
        <div id="result" style="$style">
        <h2><div class="alert-box error"><span>error: </span>'.$captcha_message.'</div></h2>
        </div>';

    } else {
        $captcha_message = "Valid captcha";
        $style = "background-color: #CCFF99";
    }


    $request_captcha = htmlspecialchars($_REQUEST['captcha']);
    unset($_SESSION['captcha']);
}
?>

<!DOCTYPE html>
<html>
    <head>
        <title>Registration with Linkvessel and collaborate with college's friends</title>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
        <script>
            $(function(){
                $("#datepicker").datepicker();
            });            
        </script>
        <style>
            .alert-box {
        color:#555;
        border-radius: 0px;
        font-family:Tahoma,Geneva,Arial,sans-serif; font-size:11px; font-size: 18px;
        padding:  30px 36px;
        margin:10px;
            }
            .alert-box span {
        font-weight:bold;
        text-transform:uppercase;
            }
            .error {
        border:2px solid #0c0b0b;
                background-color: #ff7e48;
            }

            .success{
            border:2px solid #0c0b0b;
            background-color: #a3ea42;
            }
        </style>
    </head>
    <body onload="document.getElementById('captcha-form').focus()">
        <div id="header">
            <img id="logo_size" src="./images/logo.png" onmousedown="return false">
        </div><br><br><br>

        <form id="form_box" method="post" action="register.php" name="registerform">
                <div id="title">
                    <h2>REGISTRATION FORM</h2>
                </div>

                <div class="controls pos_selectbox">
                    <select id="basic" name="user_college" class="input-medium">
                        <option>Select College</option>
                        <option>MAIIT kota</option>
                    </select>&nbsp; &nbsp;

                    <select id="basic" name="user_branch" class="input-medium">
                        <option>Select Branch</option>
                        <option>Computer science</option>
                        <option>Civil</option>
                        <option>Mechanical</option>
                        <option>Electrical</option>
                        <option>Bioinformatic</option>
                    </select>&nbsp; &nbsp;

                    <select id="basic" name="user_year" class="input-medium">
                        <option>Select year</option>
                        <option>1st year</option>
                        <option>2nd year</option>
                        <option>3rd year</option>
                        <option>4th year</option>
                        <option>Year completed</option>
                    </select>&nbsp; &nbsp;
                </div><br>

                <input id="input_pos" type="email" name="user_email" required="" placeholder="Email address" /><br><br>

                <input id="input_pos" type="password" name="user_password_new" required="" placeholder="Password" /><br><br>

                <input id="input_pos" type="password" name="user_password_repeat" required="" placeholder="Confirm password" /><br><br>

                <input id="name_pos" type="text" name="user_firstname" required="" placeholder="First name" />&nbsp;

                <input id="name_pos2" type="text" name="user_lastname" required="" placeholder="Last name" /><br><br>

                <input id="datepicker" type="text" name="user_dob" required="" placeholder="Date of birth" /><br><br>

                <input id="name_pos" type="text" name="user_state" required="" placeholder="State" />&nbsp;

                <input id="name_pos2" type="text" name="user_city" required="" placeholder="city" />&nbsp;<br><br>

                <img src="captcha.php" id="captcha" /><br/>

                <a href="#" onclick="
    document.getElementById('captcha').src='captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>

    <input type="text" name="captcha" id="captcha-form" autocomplete="off" placeholder="Type code here" /><br><br>

                <input type="submit" name="register" id="pos_submit" class="btn btn-primary btn-large" value="Create account.."/>
            </form>
</html>

EDITED

<?php
    // checking for minimum PHP version
    if (version_compare(PHP_VERSION, '5.3.7', '<')) {
        exit("Sorry, Simple PHP Login does not run on a PHP version smaller than 5.3.7 !");
    } else if (version_compare(PHP_VERSION, '5.5.0', '<')) {
        // if you are using PHP 5.3 or PHP 5.4 you have to include the password_api_compatibility_library.php
        // (this library adds the PHP 5.5 password hashing functions to older versions of PHP)
        require_once("libraries/password_compatibility_library.php");
    }

    // include the configs / constants for the database connection
    require_once("config/db.php");

    // load the registration class
    require_once("classes/Registration.php");

    // create the registration object. when this object is created, it will do all registration stuff automatically
    // so this single line handles the entire registration process.
    $registration = new Registration();

    // show the register view (with the registration form, and messages/errors)
    include("views/register.php");

?>

1 个答案:

答案 0 :(得分:0)

简单的例子,删除register.php并在Else上添加有关数据库的信息,如果验证码是正确的。

/** Validate captcha */
if (!empty($_REQUEST['captcha'])) {
    if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha']) {
        $captcha_message ="Invalid captcha";
        $style = "background-color: #FF606C color:#555
        border-radius: 0px
        font-family:Tahoma,Geneva,Arial,sans-serif font-size:11px;
        font-size: 18px
        padding:  30px 36px
        margin:10px font-weight:bold
        text-transform:uppercase border:2px solid #0c0b0b
        background-color: #ff7e48 ";

        echo'
        <div id="result" style="$style">
        <h2><div class="alert-box error"><span>error: </span>'.$captcha_message.'</div></h2>
        </div>';

    } else {
        $captcha_message = "Valid captcha";
        $style = "background-color: #CCFF99";
       //insert in database here and redirect to index or profil
    }
    $request_captcha = htmlspecialchars($_REQUEST['captcha']);
    unset($_SESSION['captcha']);
}

并替换

<form id="form_box" method="post" action="register.php" name="registerform">

<form id="form_box" method="post" action="" name="registerform">
相关问题