I am new to this forum and hope that I have found a good PHP source for helping me when I am situations like this. I am working on a registration/validation form at the moment. So far all is going well with the exception of this one thing; Warning: Invalid argument supplied for foreach()!
This piece of code is what I used in the body of my page to display any possible error when submitting the registration form.
Code: Select all
<?php
if (isset($message)) {
echo '<ul>';
foreach ($message as $item) {
echo "<li>$item</li>";
}
echo '</ul>';
}
?>
Code: Select all
<?php
if (array_key_exists('register', $_POST)) {
// remove backslashes from the $_POST array
include('includes/corefuncs.php');
include('includes/conn.inc.php');
nukeMagicQuotes();
// Check The Length Of The Username
$username = trim($_POST['username']);
// Check The Length Of The Password
$password = trim($_POST['password']);
// Check Email
$email = trim($_POST['email']);
// Initialize Error Array
$message = array();
// Check length of username
if (strlen($username) < 6 || strlen($username) > 15) {
$message[] = 'Username must be between 6 and 15 characters';
}
// Validate username
if (!ctype_alnum($username)) {
$message[] = 'Username must consist of alphanumeric characters with no spaces';
}
// Check password
//if (strlen($password) < 6 || preg_match('/\s/', $password)) {
//$message[] = 'Password must be at least 6 characters with no spaces';
//}
// Check that the passwords match
//if ($password != $_POST['conf_pwd']) {
//$message[] = 'Your passwords don\'t match';
//}
// If no errors so far, check for duplicate username
if (!$message) {
// Connect to database as administrator
$conn = dbConnect('admin');
// Admin check for duplicate username
$checkDuplicate = "SELECT login_id FROM login
WHERE username = '$username'";
$userResult = mysql_query($checkDuplicate) or die(mysql_error());
$numRows = mysql_num_rows($userResult);
// If $numRows is positive, the username is already in use
if ($numRows) {
$message[] = "$username is already in use. Please choose another username.";
}
if (!$message) {
// Connect to database as administrator
$conn = dbConnect('admin');
// Admin check for duplicate email
$checkDuplicate = "SELECT login_id FROM login
WHERE email = '$email'";
$emailResult = mysql_query($checkDuplicate) or die(mysql_error());
$numRows = mysql_num_rows($emailResult);
// If $numRows is positive, the username is already in use
if ($numRows) {
$message[] = "$email is already in use. Please choose another email address.";
}
else {
if ($_POST['form_submitted'] == '1') {
// Generate Password
function createPassword($length) {
$chars = "234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$i = 0;
$password = "";
while ($i <= $length) {
$password .= $chars{mt_rand(0,strlen($chars))};
$i++;
}
return $password;
}
$password = createPassword(8);
// User is registering, insert data until we can activate it
$activationKey = mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
$sql="INSERT INTO login (username, password, email, activationkey, status) VALUES ('$_POST[username]', '$password', '$_POST[email]', '$activationKey', 'verify')";
if (!mysql_query($sql))
{
die('Error: ' . mysql_error());
}
echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";
///////////// Send activation Email
$to = $_POST[email];
$subject = " admin@website.com Registration";
$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at key2jhousing.com. You can complete registration by clicking the following link:\rhttp://www.key2housing.com/thefuture/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards, website.com Team";
$headers = 'From: noreply@website.com' . "\r\n" .
'Reply-To: noreply@website.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
}
}
}
}
?>
Appreciate any help on this,
Will