|
PHP Registration - [TUT #2]
We'll be using the data from my last tutorial here <- Clicky
check.php (To check if they can have that username or not)
PHP Code:
<?php
include ("database.php");
function showError($errorr){
echo "<b><font color=\"red\">".$errorr."</font></b><br /><br /><br >";
include 'form.php';
}
if(!isset($_POST['username']) || !isset($_POST['password'])){
showError("You did not set the username or the password field."); die();
}
$username = $_POST['username'];
$password = $_POST['password'];
$row1 = mysql_query("SELECT * FROM `user` WHERE `username` = '". $username ."'");
$check1 = mysql_num_rows($row1);
if($check1 != 0){
showError("This username is already in use."); die();
}
$validname = false;
$validpass = false;
$namelength = strlen($username);
$passlength = strlen($password);
$validchars = array('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', ' ', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
for ($i = 0; $i < $namelength; $i++) {
for($b = 0; $b < count($validchars); $b++){
if($username[$i] == $validchars[$b]){
$validname = true;
}
}
}
for ($i = 0; $i < $passlength; $i++) {
for($b = 0; $b < count($validchars); $b++){
if($password[$i] == $validchars[$b]){
$validpass = true;
}
}
}
if($namelength < 5 || $namelength > 12 || $validname == false){
showError("Invalid username, your username should be between 5 and 12 characters and should not contain any special characters."); die();
}
if($passlength < 5 || $passlength > 20 || $validpass == false){
showError("Invalid password, your password should be between 5 and 20 characters and should not contain any special characters."); die();
}
if($validname && $validpass){
@mysql_query("INSERT INTO `user` (`username` , `password` , `rights`) VALUES ('".$username."', '".$password."','0')") or die(mysql_error());
$fp = @fopen( "account_logs.txt", "a" );
@fputs($fp, "ACCOUNT CREATED: ".getEnv("REMOTE_ADDR")"\n" );
echo "Account created successfully!";
} else {
echo "Error, please contact the administrator";
}
?>
Form.php (With the form on it, obvisouly)
PHP Code:
<form method="post" action="check.php" name="reg">
Username: <input type="text" name="username" maxlength="12"><br />
Password: <input type="password" name="password" maxlength="20"><br />
<input type="submit" value="Check Username" name="submit">
</form>
Breaking down check.php
PHP Code:
if(!isset($_POST['username']) || !isset($_POST['password'])){
showError("You did not set the username or the password field."); die();
}
If the user didn't put a value for the fields, it'll show this error.
PHP Code:
$username = $_POST['username'];
$password = $_POST['password'];
$row1 = mysql_query("SELECT * FROM `user` WHERE `username` = '". $username ."'");
$check1 = mysql_num_rows($row1);
if($check1 != 0){
showError("This username is already in use."); die();
}
Gets the value from the fields and checks if they are in use
PHP Code:
$validname = false;
$validpass = false;
$namelength = strlen($username); //Returns the length of the given string .
$passlength = strlen($password); //Returns the length of the given string .
$validchars = array('a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J', 'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'O', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', ' ', '_', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
for ($i = 0; $i < $namelength; $i++) {
for($b = 0; $b < count($validchars); $b++){
if($username[$i] == $validchars[$b]){
$validname = true;
}
}
}
for ($i = 0; $i < $passlength; $i++) {
for($b = 0; $b < count($validchars); $b++){
if($password[$i] == $validchars[$b]){
$validpass = true;
}
}
}
All this is used to check if the username/password is valid, if it's the amount of characters that are needed and if there's an invalid character in it, (say ~ for example)
PHP Code:
if($namelength < 5 || $namelength > 12 || $validname == false){
showError("Invalid username, your username should be between 5 and 12 characters and should not contain any special characters."); die();
}
if($passlength < 5 || $passlength > 20 || $validpass == false){
showError("Invalid password, your password should be between 5 and 20 characters and should not contain any special characters."); die();
}
If it's not the right length or it contains an invalid character it will give this error.
PHP Code:
if($validname && $validpass){
@mysql_query("INSERT INTO `user` (`username` , `password` , `rights`) VALUES ('".$username."', '".$password."','0')") or die(mysql_error());
$fp = @fopen( "../logging/accounts.txt", "a" );
@fputs($fp, "ACCOUNT CREATED: ".getEnv("REMOTE_ADDR") "\n");
echo "Account created successfully!";
} else {
echo "Error, please contact the administrator";
}
If everything's ok, insert into the database
Nothing to explain in Form.php, it's just a form.
Demo
Please visit my friends Web site to keep me posting tutorials 
Last edited by PeeHPee; 04-11-2008 at 12:52 AM.
|