我需要检查用户名是否被占用,如果是,它将不允许用户注册它将它们重定向到注册页面 我应该在哪里添加脚本以检查用户名是否已被删除?或者我在哪里可以得到一个脚本 继承我的剧本我有什么安慰。
<?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();
}
?>
答案 0 :(得分:3)
您需要先检查您的用户名是否存在:
SELECT count(1) FROM members WHERE username = '$_POST[username]'
然后如果结果是&gt; 0您抛出User already exists
错误。
答案 1 :(得分: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
}
}
?>
答案 2 :(得分: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!");
}
}
?>