Hello,
I'm redoing an old project of mine where I created a forum from scratch. I recently finished coding the registration page for the forum, and I was wondering if someone can give some tips or advice on it. I'm trying to learn best practices since I'm basically a self-taught PHP coder (I studied .NET in college).
Also note all the input fields are checked via JavaScript before being submitted to the server.
Here's the code:
<?php
include("functions.php");
$message = '';
if(isset($_REQUEST['submit']))
{
if(strlen(trim($_REQUEST['name'])) < 4 || strlen(trim($_REQUEST['name'])) > 20)
$message = 'Your user name must be between 4 and 20 characters long.<br />';
if(strlen(trim($_REQUEST['pass1'])) < 6)
$message .= 'Your password must be at least 6 characters long.<br />';
if($_REQUEST['pass1'] != $_REQUEST['pass2'])
$message .= 'Your passwords do not match.';
if(preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $_REQUEST['email1']) < 1)
$message .= 'Please enter a valid email address. This email is private and only available to staff. If you choose, you may make it public in the privacy settings.<br />';
if($_REQUEST['email1'] != $_REQUEST['email2'])
$message .= 'Your emails do not match.';
if(strlen(trim($message)) == 0)//All input has been validated
{
if(CheckValidUser($_REQUEST['name']))//User name is not in use and not on the reserved list
{
$conn = Connect("R");
$query = "INSERT INTO users (perm_id, user_name, user_pass, user_postcount, user_register, user_title, user_ip, user_online)";
$query .= " VALUES(2, '".addslashes($_REQUEST['name'])."', '".sha1(md5($_REQUEST['pass1']))."', 0, NOW(), 'Member', '".$_SERVER['REMOTE_ADDR']."', 0)";
$result = mysql_query($query, $conn);
if(!$result)//There was an error inserting the user into the database, so close the connection and display the error message
{
mysql_close($conn);
$message = 'There was an error with the registration. Please try again later.';
}
else//Insertion of the user was successful, so now insert the user's info into the dependent tables
{
$id = mysql_insert_id($conn);
mysql_query("INSERT INTO avatars VALUES(NULL, ".$id.", NULL, NULL, NULL)", $conn);
mysql_query("INSERT INTO details VALUES(NULL, ".$id.", 4, '".addslashes($_REQUEST['email1'])."', NULL, NULL, NULL, NULL)", $conn);
mysql_query("INSERT INTO display_settings VALUES(NULL, ".$id.", 50, 50, 50, 1)", $conn);
mysql_query("INSERT INTO privacy_settings VALUES(NULL, ".$id.", 1, 1, 0, 0)", $conn);
mysql_query("INSERT INTO profiles VALUES(NULL, ".$id.", NULL, NULL)", $conn);
mysql_query("INSERT INTO settings VALUES(NULL, ".$id.", 1, 1, 0, 0)", $conn);
mysql_close($conn);
UpdateIPHistory($id, $_SERVER['REMOTE_ADDR'], "R");
}
}
else
$message = 'That user name is not available.';
}
}
?>
If you have any questions please ask. Thanks in advance! 🙂