Forum Moderators: coopster

Message Too Old, No Replies

validating usernames

need to keep them unique

         

Cobra123

12:03 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



i need a way to stop more than one user having the same username or/and email, like on these forums when u register.

if u could help me out that would be great, thanx,

Cobra

ergophobe

2:10 pm on Jul 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When the user registers, just check against your list of current users. If there's a collision, refuse to register, send them back to the registration page they came from and add an error message saying

"This username is already in use. Please try another."

You obviously must already be as far as getting a list of users and checking against it, otherwise your login wouldn't work, so let's skip that part. In terms of sending them back to the registration page, you can use

header("Location: reg_page.php");
exit();

As for the error message, you can handle that many ways but very easy would be to send them back using

header("Location: reg_page.php?error=name_taken");
exit();

Then in reg_page.php

if (isset($_GET['error']) && $_GET['error'] == 'name_taken') {
$msg = "This username is already in use. Please try another.";
}

Tom

Cobra123

2:28 pm on Jul 1, 2004 (gmt 0)

10+ Year Member



im sorry i dont think i explained it correctly, my bad.

ill try again :p

i need a script or something that will stop duplicate users and/or emails being entered into my MySQL database.
this is the script im using to enter the data into the database:

file=signup.html

<form action="make.php" method="post">
Username Desired: <input type="text" name="username" size="10">
Password Desired: <input type="password" name="password" size="10">
E-mail: <input type="text" name="email" size="10">
<input type="submit" value="submit" name="submit">
</form>

which takes u to here:

<?
$conn = mysql_connect("localhost","USERNAME","PASSWORD");

$db = mysql_select_db("users");

$username = $_POST["username"];
$password = $_POST["password"];
$email = $_POST["email"];

$result= MYSQL_QUERY("INSERT INTO users (id, username, password, email)".
"VALUES ('NULL', '$username', '$password', '$email')");

echo "Your name and password have been submitted into our database.";
?>

which puts the data into the database which in turn allows u to login via the login page. i need something in the 2nd script that will allow me to stop ppl entering duplicate usernames or email.

PS. this isnt meant to look good or anything atm, im just testing / tweaking things for my skool project.

thanx for ur help.

timster

3:09 pm on Jul 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This recent thread may help:

[webmasterworld.com ]

dreamcatcher

5:03 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could just query the database and if it returns any rows, display an error message like ergophobe mentioned. The way I like to do it is to use an array for the error string.

$query = SELECT FROM users WHERE username = '$username'";
$result = mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result)>0)

{
$error_string[] = "Username already exists, please try again!<br>";
}

$query2 = SELECT FROM users WHERE password = '$password'";
$result2 = mysql_query($query2) or die(mysql_error());

if (mysql_num_rows($result2)>0)

{
$error_string[] = "Password already exists, please try again!";
}

if (!empty($error_string))
{
foreach ($error_string as $message)
{
echo $message;
}
exit;
}

:)