Reputation: 21
I'm new to php and I created a user registration form. But whenever I submit the form, part of my page doesn't load. I see the "successful registration" message, but I don't see my right sidebar, footer, or my wrapper div. After searching the internet all day, I concluded it was the exit() function. But if I leave in the exit function and submit the form, all of my page loads, but the original registration form is still shown below my "successful registration" message. Can someone please help?
<?php
require_once('_includes/connectvars.php');
if (isset($_POST['submitted'])) {
$firstname = mysql_real_escape_string(trim($_POST['firstname']));
$lastname = mysql_real_escape_string(trim($_POST['lastname']));
$email = mysql_real_escape_string(trim($_POST['email']));
$birthdate = mysql_real_escape_string(trim($_POST['birthdate']));
$zipcode = mysql_real_escape_string(trim($_POST['zipcode']));
$gender = $_POST['gender'];
$username = mysql_real_escape_string(trim($_POST['username']));
$password1 = mysql_real_escape_string(trim($_POST['password1']));
$password2 = mysql_real_escape_string(trim($_POST['password2']));
$terms = $_POST['terms'];
$newsletter = $_POST['newsletter'];
$activationkey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$captchchk = 1;
require_once('_includes/recaptchalib.php');
$privatekey = "some#";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (preg_match ('%^[A-Za-z\.\' \-]{2,15}$%', stripslashes(trim($_POST['firstname'])))) {
$firstname = escape_data($_POST['firstname']);
} else {
$firstname = FALSE;
echo '<p class="error">Please enter a valid first name!</p>';
}
if (preg_match ('%^[A-Za-z\.\' \-]{2,30}$%', stripslashes(trim($_POST['lastname'])))) {
$lastname = escape_data($_POST['lastname']);
} else {
$lastname = FALSE;
echo '<p class="error">Please enter a valid last name!</p>';
}
if (preg_match('%^(0?[1-9]|1[012])[/](0?[1-9]|[12][0-9]|3[01])[/](19|20)?[0-9]{2}$%', stripslashes(trim($_POST['birthdate'])))) {
$birthdate = escape_data($_POST['birthdate']);
} else {
$birthdate = FALSE;
echo '<p class="error">Please enter a valid date of birth in the format of MM/DD/YYYY</p>';
}
if (preg_match ('%^[0-9]{5}$%', stripslashes(trim($_POST['zipcode'])))) {
$zipcode = escape_data($_POST['zipcode']);
} else {
$zipcode = FALSE;
echo '<p class="error">Please enter a valid 5 digit zip code!</p>';
}
if ($gender !== 'M') {
if ($gender !== 'F') {
echo '<p class="error">Please select your gender!</p>';
}
}
if (preg_match ('%^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*(\S{4,75})$%', stripslashes(trim($_POST['username'])))) {
$username = escape_data($_POST['username']);
} else {
$username = FALSE;
echo '<p class="error">Please enter a valid username!</p>';
}
if (preg_match ('%^[A-Za-z0-9]{6,15}$%', stripslashes(trim($_POST['password1'])))) {
$password1 = escape_data($_POST['password1']);
if (($_POST['password1'] == $_POST['password2']) && ($_POST['password1'] != $_POST['username'])) {
$password1 = escape_data($_POST['password1']);
} elseif ($_POST['password1'] == $_POST['username']) {
$password1 = FALSE;
echo '<p class="error">Your password cannot be the same as the username!</p>';
} else {
$password1 = FALSE;
echo '<p class="error">Your password did not match the confirmed password!</p>';
}
} else {
$password1 = FALSE;
echo '<p class="error">Please enter a valid password!</p>';
}
if ($terms !== 'Y') {
echo '<p class="error">You must agree to the terms of use!</p>';
}
if (!$resp->is_valid) {
echo '<p class="error">The CAPTCHA Code wasn\'t entered correctly!</p>';
$captchchk = 0;
}
if ($firstname && $lastname && $email && $birthdate && $zipcode && $gender && $username && $password1 &&
$terms && $captchchk) {
$query = "SELECT * FROM members WHERE username = '$username'";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
if(mysql_num_rows($result) == 1) {
echo '<br><br><p class="error">An account already exists for this username. Please select a different
username.</p>';
$username = "";
} else {
if(mysql_num_rows($result) == 0) {
$query = "INSERT INTO members (first_name, last_name, email, dob, zip_code, gender, username, password, join_date, newsletter, active, terms, status) VALUES ('$firstname', '$lastname', '$email', STR_TO_DATE('$birthdate', '%m/%d/%Y'), '$zipcode', '$gender', '$username', SHA('$password1'), NOW(), '$newsletter', '$activationkey', '$terms', 'verify')";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
if (mysql_affected_rows() == 1) {
$to = $_POST['email'];
$subject = "Registration";
$message = "Welcome to Mysite.com!\n\nYou, or someone using your email address, has completed registration at Mysite.com. You can complete registration by clicking the following link:\n\nhttp://www.mysite.com/verify.php?$activationkey\n\nIf this is an error, ignore this email and you will be removed from our mailing list.\n\nRegards,\n\nThe Mysite.com Team";
$headers = 'From: noreply@ Mysite.com' . "\r\n" .
'Reply-To: noreply@ Mysite.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
echo '<br /><br /><p>Thank you for registering! A confirmation email has been sent to your email. Please click on the link in that email in order to activate your account.</p>';
} else {
$queryString = $_SERVER['QUERY_STRING'];
$query = "SELECT * FROM members";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
while($row = mysql_fetch_array($result)){
if ($queryString == $row["active"]){
echo "Congratulations!" . $row["username"] . " is now a member of Mysite.com!";
$query="UPDATE members SET active = '', status='activated' WHERE (user_id =
$row[user_id])";
if (!mysql_query($query)) {
die('Error: ' . mysql_error());
}
}
}
}
}
}
}
exit();
mysql_close();
}
Upvotes: 2
Views: 536
Reputation: 7086
There are two solutions.
Upvotes: 1
Reputation: 154673
You don't need exit()
, just a if ... else
block:
if ((/* form submitted */) && (/* everything is OK */))
{
// do stuff
}
else
{
// show form
}
Upvotes: 1