Okay.
First I'll show the files in question:
index.php (code snippet)
<?php
if (isset($_POST['submit']) && $_POST['submit'] == 'Register')
{
reg_results();
}
if ($_SESSION['LoggedIn'] == 1)
{
echo '
Welcome <font color="#003366">' . $_SESSION['username'] . '</font> , you are already a registered member.<br><br>
';
}
else
{
register_form();
}
?>
functions.php
function register_form( $txt= "", $username = "", $password = "", $email = "", $location = "", $fav_rapper = "", $info = "" )
{
echo "
<form action=\"?cmd=reg_results\" method=\"post\">
<div align=\"left\">
<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td>
<center>
<font color=\"red\">$txt</font>
</center>
</td>
<td> </td>
</tr>
<tr>
<td><font color=\"#FF0000\">*</font> Username :\n<br>
<br>
</td>
<td>
<input type=\"text\" name=\"username\" size=\"20\" maxlength=\"20\">
<br>
</td>
</tr>
<tr>
<td><font color=\"#FF0000\">*</font> Password :\n<br>
<br>
</td>
<td>
<input type=\"password\" name=\"password\" size=\"20\" maxlength=\"20\">
<br>
</td>
</tr>
<tr>
<td><font color=\"#FF0000\">*</font> Email Address :\n<br>
<br>
</td>
<td>
<input type=\"text\" name=\"email\" size=\"20\" maxlength=\"200\">
<br>
</td>
</tr>
<tr>
<td>Location :\n<br>
<br>
</td>
<td>
<input type=\"text\" name=\"location\" size=\"20\" maxlength=\"20\">
<br>
</td>
</tr>
<tr>
<td>Favourite rapper :\n<br>
<br>
</td>
<td>
<input type=\"text\" name=\"fav_rapper\" size=\"20\" maxlength=\"20\">
<br>
</td>
</tr>
<tr>
<td>Further Info :\n<br>
<br>
</td>
<td>
<textarea name=\"info\" cols=\"20\" rows=\"3\" maxlength=\"100\"></textarea>
<br>
</td>
</tr>
<tr>
<td><font color=\"#FF0000\">*</font> Denotes required fields\n</td>
<td> </td>
</tr>
<tr>
<td>
<input type=\"hidden\" name=\"cmd\" value=\"reg_results\">
<input type=\"submit\" value=\"Register\" name=\submit\>
</td>
<td> </td>
</tr>
</table>
</div>
</form>";
}
function reg_results()
{
// Define post fields into simple variables
$username = $_POST['username'];
$password = $_POST['password'];
$email = $_POST['email'];
$location = $_POST['location'];
$fav_rapper = $_POST['fav_rapper'];
$info = $_POST['info'];
/* Let's strip some slashes in case the user entered
any escaped characters. */
$username = stripslashes($username);
$password = stripslashes($password);
$email = stripslashes($email);
$location = stripslashes($location);
$fav_rapper = stripslashes($fav_rapper);
$info = stripslashes($info);
/* Do some error checking on the form posted fields */
if((!$email) || (!$password) || (!$username)){
$txt = "You did not submit the following required information! <br />";
if(!$username){
$txt = "Username is a required field. Please enter it below.<br />";
}
if(!$password){
$txt = "Password is a required field. Please enter it below.<br />";
}
if(!$email){
$txt = "Email is a required field. Please enter it below.<br />";
}
register_form( $txt, $username, $password, $email, $location, $fav_rapper, $info );
/* End the error checking and if everything is ok, we'll move on to
creating the user account */
exit(); // if the error checking has failed, we'll exit the script!
}
/* Let's do some checking and ensure that the user's email address or username
does not exist in the database */
$sql_email_check = mysql_query("SELECT email FROM cms_users
WHERE email='$email'");
$sql_username_check = mysql_query("SELECT username FROM cms_users
WHERE username='$username'");
$email_check = mysql_num_rows($sql_email_check);
$username_check = mysql_num_rows($sql_username_check);
if(($email_check > 0) || ($username_check > 0)){
echo "Please fix the following errors: <br />";
if($email_check > 0){
$txt = "Your email address has already been used by another member
in our database. Please submit a different Email address!<br />";
unset($email);
}
if($username_check > 0){
$txt = "The username you have selected has already been used by another member
in our database. Please choose a different Username!<br />";
unset($username);
}
register_form( $txt, $username, $password, $email, $location, $fav_rapper, $info );
exit(); // exit the script so that we do not create this account!
}
/* Everything has passed both error checks that we have done.
It's time to create the account! */
// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO cms_users (username, password, email, location, fav_rapper, info, user_level, reg_date)
VALUES('$username', '$password','$email','$location','$fav_rapper','$info','1',now())")
or die (mysql_error());
if(!$sql){
$txt = "There has been an error creating your account. Please contact the webmaster.";
} else {
$user_id = mysql_insert_id();
// Let's mail the user!
$subject = "Your Membership at HipHopReaction!";
$message = "Dear $username,
Thank you for registering at our website, [url]http://www.hiphopreaction.com[/url]!
You will be able to login with the following information:
Username: $username
Password: $password
Thanks!
The Webmaster
This is an automated response, please do not reply!";
mail($email, $subject, $message,
"From: MyDomain Webmaster<admin@hiphopreaction.com>\n
X-Mailer: PHP/" . phpversion());
$txt = "Your membership information has been mailed to your email address! You may now Login";
}
}
Problem is, the form shows successfully when meant to, but the error messages held in the variable $txt are not sent back and displayed.
Any help would be really appreciated and would in fact finish my register/login script
🙂