0

我需要检查用户名是否被使用,如果是,它不会允许用户注册,它会将他们重定向到注册页面我应该在哪里添加脚本来检查用户名是否已经被使用?或者我在哪里可以得到一个脚本继承我的脚本我到目前为止。

 <?php

// Check if he wants to register:
if (!empty($_POST[username]))
{
// Check if passwords match.
if ($_POST[password] != $_POST[password2])
    exit("Error - Passwords don't match. Please go back and try again.");

// Assign some variables.
$date = time (" d - m - Y ");
$ip = $_SERVER[REMOTE_ADDR];

require_once("connect.php");

// Register him.
$query = mysql_query("INSERT INTO members 
(username, fname, email, password, date, ip)
VALUES      ('$_POST[username]','$_POST[fname]','$_POST[email]','$_POST[password]','$date','$ip')")
or die ("Error - Couldn't register user.");

echo "Welcome $_POST[username]! You've been successfully reigstered!<br />
    You Will Be Redirected To Our Home Page Where U Can Login ";
exit();
}

?>
4

3 回答 3

3

您需要先检查您的用户名是否存在:

SELECT count(1) FROM members WHERE username = '$_POST[username]'

然后如果结果是 > 0 你会抛出一个User already exists错误。

于 2012-11-21T00:08:48.253 回答
0

我对您的代码进行了一些更改,以使其正常工作并清理它。

<?php
// Check if he wants to register
// - Make sure that param exists
// - Check length 
if (isset($_POST['username']) && strlen($_POST['username']) > 0) {

    // Check if passwords match.
    // - Make sure that params exist
    // - Check length 
    // - Check matching
    if (!isset($_POST['password']) || 
        !isset($_POST['password2']) ||
        strlen($_POST['password']) === 0 || 
        $_POST['password'] !== $_POST['password2']) {
           die("Error - Passwords don't match. Please go back and try again.");
    }

    require_once("connect.php");

    // Set array of params
    $fields = array(
        'username' => $_POST['username'],
        'fname' => isset($_POST['fname']) ? $_POST['fname'] : '',
        'email' => isset($_POST['email']) ? $_POST['email'] : '',
        'password' => $_POST['password'],
        'date' => date("d-m-Y"),
        'ip' => $_SERVER[REMOTE_ADDR]
    );

    // Escape fields agains sql injection
    $fields = array_map('mysql_real_escape_string', $fields);

    // Check if member exists
    $exists = mysql_num_rows(mysql_query("SELECT * FROM members WHERE username='" . $fields['username'] . "'"));
    if ($exists === 0) {

        // Insert member in DB
        if (mysql_query("INSERT INTO `members` (`" . implode('`,`', array_keys($fields)) . "`) VALUES ('" . implode("','", array_values($fields)) . "')") !== false) {
            echo "Welcome" . $_POST['username'] . "! You've been successfully reigstered!<br />
            You Will Be Redirected To Our Home Page Where U Can Login ";
            exit();
        } else {
            die("Error - Couldn't register user.");
        }
    } else {
        die("Error - Member exists!");
    }
}
?>
于 2012-11-21T01:14:12.760 回答
0

我更新了您的代码,我可能会检查是否使用了用户名。就像其他人所说的那样,阅读 sql 注入攻击并保护自己。

<?php

// Check if he wants to register:
if (!empty($_POST[username])) {
// Check if passwords match.
    if ($_POST[password] != $_POST[password2])
        exit("Error - Passwords don't match. Please go back and try again.");

// Assign some variables.
    $date = time(" d - m - Y ");
    $ip = $_SERVER[REMOTE_ADDR];

    require_once("connect.php");

    // CHEK IF USERNAME IS VALID
    if (someFunctionThatChecksIfUsernameIsTaken($_POST['username'])) {
        // Register him.
        $query = mysql_query("INSERT INTO members 
        (username, fname, email, password, date, ip)
        VALUES      ('$_POST[username]','$_POST[fname]','$_POST[email]','$_POST[password]','$date','$ip')")
                or die("Error - Couldn't register user.");

        echo "Welcome $_POST[username]! You've been successfully reigstered!<br />
        You Will Be Redirected To Our Home Page Where U Can Login ";
        exit();
    } else {
        // redirect him back to the page and tell the user that the username is taken
    }
}
?>
于 2012-11-21T00:11:54.427 回答