I just wrote something based off of what you posted, but it is quite different and will probably look confusing. I'll do my best to not leave you confused

Here is the code in its entirety:
Here's a file I created called success.php:
CODE
<?php
function clean($string) {
$string = trim($string);
$string = stripslashes($string);
return $string;
}
function user_register($username, $email, $day, $month, $year, $password) {
$message = "Username: $username<br/ >";
$message .= "Password: $password<br/ >";
$message .= "Email: $email<br/ >";
$message .= "Date of Birth: $day-$month-$year<br/ >";
$message .= "You have successfully registered!";
return $message;
}
$count = NULL;
$error = array(
'Your username must only contain letter and numbers and be at least 5 characters but no longer than 12 characters in length!',
'Your password must only contain letter and numbers and be at least 6 characters but no longer than 12 characters in length!',
'Your passwords do not match',
'The e-mail you entered was not in the proper format!',
);
if (isset($_POST['submit'])) {
$username = preg_match('/^[a-z\d]{5,12}$/i', $_POST['username']) ? clean($_POST['username']) : $count++;
$password = preg_match('/^[a-z\d]{6,12}$/i', $_POST['password']) ? clean($_POST['password']) : $count++;
$confirm_pw = ($_POST['password'] == $_POST['confirmpassword']) ? clean($_POST['confirmpassword']) : $count++;
$email = preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $_POST['email']) ? clean($_POST['email']) : $count++;
$day = $_POST['dobday'];
$month = $_POST['dobmonth'];
$year = $_POST['dobyear'];
if ($count) {
echo "There are errors:<br />";
for ($i=0; $i <= $count; $i++) {
echo $error[$i] . "<br />";
}
echo "Please go back and re-submit your form.<br/>";
}
else {
echo user_register($username, $email, $day, $month, $year, $password);
}
}
else {
echo "You cannot access this page directly.";
//header("Location: index.php");
}
Here's a mock registration form called form.php:
CODE
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="success.php" method="post">
<div>
<label for="Username">Username:</label>
<input type="text" name="username" id="username"/>
</div>
<div>
<label for="Password">Password:</label>
<input type="password" name="password" id="password"/>
</div>
<div>
<label for="Confirm Password">Confirm Password:</label>
<input type="password" name="confirmpassword" id="confirmpassword"/>
</div>
<div>
<label for="Email">Email:</label>
<input type="text" name="email" id="email"/>
</div>
<div>
<label for="Date of Birth">Date of Birth (DD/MM/YYYY):</label>
<input type="text" name="dobday" id="dobday"/>
<input type="text" name="dobmonth" id="dobmonth"/>
<input type="text" name="dobyear" id="dobyear"/>
</div>
<div>
<input type="submit" name="submit" id="submit" value="Submit"/>
</div>
</form>
</body>
</html>
And here's success.php in pieces...
First off I created a small function called clean()
CODE
function clean($string) {
$string = trim($string);
$string = stripslashes($string);
return $string;
}
This will take what's passed to it and remove white space from the beginning and end of the string, strip backslashes from the string, and then return a new, "clean" string to work with. It's good practice to do something similar to all form input via a user.
You'll see a I added a function called user_register() just so I had something to return if the form was successfully submitted.
CODE
function user_register($username, $email, $day, $month, $year, $password) {
$message = "Username: $username<br/ >";
$message .= "Password: $password<br/ >";
$message .= "Email: $email<br/ >";
$message .= "Date of Birth: $day-$month-$year<br/ >";
$message .= "You have successfully registered!";
return $message;
}
All it does is return a string called $message that contains the input from the form.
Next you'll see a variable called $count and an array called $error
CODE
$count = NULL;
$error = array(
'Your username must only contain letter and numbers and be at least 5 characters but no longer than 12 characters in length!',
'Your password must only contain letter and numbers and be at least 6 characters but no longer than 12 characters in length!',
'Your passwords do not match',
'The e-mail you entered was not in the proper format!',
);
$count is set to NULL initially because if there are errors, it will begin to keep track of them for us in a very generic way. If there are no errors, it just stays empty (null). Note: you may think setting $count to 0 initially is the same as setting it to NULL, but in this small script, if it's set to 0, an error will always be printed. This probably isn't the most efficient way to do error checking anyway, but it's something to get started with.
$error is an array of your error messages. Notice they don't have keys hard-coded, so to access them you'd just have to do $error[0], $error[1], etc. This is where the problem with $count = 0 comes in. If $count always equals 0, the first value of the array will always be printed and your form will never be successfully submitted. This should probably be changed like I said above, but moving on...
Here's the opening of the main if block...
CODE
if (isset($_POST['submit'])) {
This says if the submit button from form.php has been pressed, do something. isset($_POST['submit']) will only return true if the button has been pressed. 'submit' refers to the value name attribute of the input button element on form.php.
This next part may be the most confusing and sorry for writing it this way, but it'll all end up the same any way you do it:
CODE
$username = preg_match('/^[a-z\d]{5,12}$/i', $_POST['username']) ? clean($_POST['username']) : $count++;
$password = preg_match('/^[a-z\d]{6,12}$/i', $_POST['password']) ? clean($_POST['password']) : $count++;
$confirm_pw = ($_POST['password'] == $_POST['confirmpassword']) ? clean($_POST['confirmpassword']) : $count++;
$email = preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $_POST['email']) ? clean($_POST['email']) : $count++;
$day = $_POST['dobday'];
$month = $_POST['dobmonth'];
$year = $_POST['dobyear'];
So the confusing parts are: $username, $password, $confirm_pw, and $email. If you look at the values of those variables, you'll see a bunch of ?'s and :'s. These are
ternary operators.
What they are really saying is:
CODE
$variable_name = condition ? if true : if false;
This is just like writing a normal if/else block:
CODE
if (condition) {
true
} else {
false
}
Anyway, what these variables are doing is all of your pattern matching. They are saying if the input from the form matches the pattern, set the variable equal to the clean version of the form input. However, if the input does not match the pattern, add 1 to $count. So, if you really don't like the look of ternary operators, you could do something like:
CODE
if (preg_match('/^[a-z\d]{5,12}$/i', $_POST['username'])) {
$username = clean($_POST['username']);
} else {
$count++;
}
...and so on for the other statements.
So next part of the script checks to see if $count is true because if it's true, it obviously contains a number, which means there are some errors:
CODE
if ($count) {
echo "There are errors:<br />";
for ($i=0; $i <= $count; $i++) {
echo $error[$i] . "<br />";
}
echo "Please go back and re-submit your form.<br/>";
}
else {
echo user_register($username, $email, $day, $month, $year, $password);
}
So if $count is true, the for loop uses $count as the index and goes through the $error array printing as many errors as $count holds. Note again, this is not the best way of doing this. It works because of the order of logic and the order the array is in. For example, if the variables above this if/else block were sorted this way..
CODE
$email = preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/', $_POST['email']) ? clean($_POST['email']) : $count++;
$password = preg_match('/^[a-z\d]{6,12}$/i', $_POST['password']) ? clean($_POST['password']) : $count++;
$username = preg_match('/^[a-z\d]{5,12}$/i', $_POST['username']) ? clean($_POST['username']) : $count++;
$confirm_pw = ($_POST['password'] == $_POST['confirmpassword']) ? clean($_POST['confirmpassword']) : $count++;
$day = $_POST['dobday'];
$month = $_POST['dobmonth'];
$year = $_POST['dobyear'];
You'd have to match the $error array to the order of how the variables are declared.
Anyway, if the form fails, you'll receive output similar to this:
QUOTE
There are errors:
Your username must only contain letter and numbers and be at least 5 characters but no longer than 12 characters in length!
Your password must only contain letter and numbers and be at least 6 characters but no longer than 12 characters in length!
Your passwords do not match
The e-mail you entered was not in the proper format!
Please go back and re-submit your form.
Now if $count is still NULL by the time the script gets to this block, your values will be passed to my version of user_register and you'll receive something like:
QUOTE
Username: woot123
Password: blah1234
Email: mail@address.com
Date of Birth: 01-12-1920
You have successfully registered!
The last part of the script prints a message to the screen if someone visits success.php without actually submitting a form (and by this I mean actually clicking the submit button):
CODE
else {
echo "You cannot access this page directly.";
//header("Location: index.php");
}
If you uncomment header("Location: index.php");, and then directly visit success.php without filling out a form, you would be redirected back to index.php right away.
Hopefully I helped a little and sorry if I was confusing. Again, there is much more you can do, probably in a better way as well, but hopefully this is a start.
Good luck.
This post has been edited by jaql: 4 Nov, 2009 - 08:53 AM