0

我在登录系统中插入 reCAPTCHA 时遇到问题,似乎用户可以输入正确的用户名和密码,甚至无需在 reCAPTCHA 中插入任何内容。一个示例登录用户是 - 用户名 = steven 密码 = steven

下面是网站的链接和登录页面的代码,然后是员工区域页面代码。

http://newmedia.leeds.ac.uk/ug10/cs10dw/workspace1/login.php

任何帮助将不胜感激。

<?php require_once("includes/connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php


    include_once("includes/form_functions.php");

    // START FORM PROCESSING
    if (isset($_POST['submit'])) { // Form has been submitted.
        $errors = array();

        // perform validations on the form data
        $required_fields = array('username', 'password');
        $errors = array_merge($errors, check_required_fields($required_fields, $_POST));

        $fields_with_lengths = array('username' => 30, 'password' => 30);
        $errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));

        $username = trim(mysql_prep($_POST['username']));
        $password = trim(mysql_prep($_POST['password']));
        $hashed_password = sha1($password);

        if ($_POST) { 
        require_once($_SERVER['DOCUMENT_ROOT'] . '/recaptcha/recaptchalib.php');
        $privatekey ="6LcHbc0SAAAAAOs2d7VnzV7RtedMkLs306ekQPUP";
        $resp = recaptcha_check_answer ($privatekey,
                            $_SERVER['REMOTE_ADDR'],
                            $_POST['recaptcha_challenge_field'],
                            $_POST['recaptcha_response_field']);
        $str_result = "";
        if (!$resp->is_valid) {
             // What happens when the CAPTCHA was entered incorrectly
            $message = "The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA   said: " . $resp->error . ")";
            // Add a redirect to an error page or just put an exit(); here

        } 

    }




        if ( empty($errors) ) {
            // Check database to see if username and the hashed password exist there.
            $query = "SELECT * ";
            $query .= "FROM users ";
            $query .= "WHERE username = '{$username}' ";
            $query .= "AND hashed_password = '{$hashed_password}' ";

            $result_set = mysql_query($query);
            confirm_query($result_set);
            if (mysql_num_rows($result_set) == 1) {
                // username/password authenticated
                // and only 1 match
                $found_user = mysql_fetch_array($result_set);
                redirect_to("staff.php");
            } else {
                // username/password combo was not found in the database
                $message = "<h1> Username or password is incorrect. </h1><br />
            ";
            }
        }
    }
?>
<?php include("includes/header.php"); ?>
<table id="structure">
    <tr>
        <td id="navigation">
            <a href="index.php">Return to public site</a>
        </td>
        <td id="page">
            <h2>Staff Login</h2>
            <?php if (!empty($message)) {echo "<p class=\"message\">" . $message . "</p>";} ?>
            <?php if (!empty($errors)) { display_errors($errors); } ?>
            <form action="login.php" method="post">
            <table>
                <tr>
                    <td>Username:</td>
                    <td><input type="text" name="username" maxlength="30" value="<?php echo htmlentities($username); ?>" /></td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><input type="password" name="password" maxlength="30" value="<?php echo htmlentities($password); ?>" /></td>
                </tr>
                <tr>

  <?php
    require_once($_SERVER['DOCUMENT_ROOT'] . '/recaptcha/recaptchalib.php');
    $publickey = "6LcHbc0SAAAAABQAnCHSHGhSuSXkZ2d1MoBa4xw2";
    echo recaptcha_get_html($publickey);
?>

                    <td colspan="2"><input type="submit" name="submit" value="Login" /></td>
                </tr>
            </table>

            </form>
        </td>
    </tr>
</table>
<?php include("includes/footer.php"); ?>

*员工页面*

<?php require_once("includes/functions.php"); ?>

<?php include("includes/header.php"); ?>
<table id="structure">
    <tr>
        <td id="navigation">&nbsp;

        </td>
        <td id="page">
            <h2>Staff Menu</h2>

            <ul>
                <li><a href="content.php">Manage Website Content</a></li>
                <li><a href="new_user.php">Add Staff User</a></li>
                <li><a href="logout.php">Logout</a></li>
            </ul>
        </td>
    </tr>
</table>
<?php include("includes/footer.php"); ?>
4

2 回答 2

0

The captcha check should be performed before redirecting to the staff page.

于 2012-05-05T20:21:00.570 回答
0

尝试这个:

    // if ($_POST) {    // Don't need this

        require_once($_SERVER['DOCUMENT_ROOT'] . '/recaptcha/recaptchalib.php');
        $privatekey ="6LcHbc0SAAAAAOs2d7VnzV7RtedMkLs306ekQPUP";
        $resp = recaptcha_check_answer ($privatekey,
                            $_SERVER['REMOTE_ADDR'],
                            $_POST['recaptcha_challenge_field'],
                            $_POST['recaptcha_response_field']);
        $str_result = "";
        if (!$resp->is_valid) {
             // What happens when the CAPTCHA was entered incorrectly
            $message = "The reCAPTCHA wasn't entered correctly. Go back and try it again. (reCAPTCHA   said: " . $resp->error . ")";
            echo $message;
            exit();

        } 

    //}
于 2012-05-05T20:28:01.100 回答