I GOT IT TO WORK! How exciting.
By the number of views it appears this is a topic of interest to others, so what follows is as much of the script as the message board will allow.
What I need to do next is move the confimation messages such as: "... echo 'Registration authentication has been mailed to your email address. Please check it for confirmation.'; ..."
If anyone wants to join in I'm building a registration/login script with all the bells and whistles: email confirmation, auto login, encryption, password change, forgotten password, user levels.
Plus, the goal is to build it on one page for easy access.
<?php # sign-in.php
// This is the registration page for the site.
// Include the configuration file for error management and such.
require_once ('./includes/config.inc');
// Set the page title, subtitle and include the header.
$page_title = 'Print X Press Register / Sign-in';
$page_subtitle = 'Register / Sign-in';
include ('./includes/header.inc');
?>
<?php
if (isset($_POST['submit1'])) { // Handle the form.
require_once ('./mysql_connect.php'); // Connect to the database.
// Check for a first name.
if (eregi ("^[[:alpha:].' -]{2,15}$", stripslashes(trim($_POST['first_name'])))) {
$fn = escape_data($_POST['first_name']);
} else {
$fn = FALSE;
$error['FirstName'] = '<font color="red" size="2">*</font>';
}
// Check for a last name.
if (eregi ("^[[:alpha:].' -]{2,30}$", stripslashes(trim($_POST['last_name'])))) {
$ln = escape_data($_POST['last_name']);
} else {
$ln = FALSE;
$error['LastName'] = '<font color="red" size="2">*</font>';
}
// Check for a title.
if (eregi ("^[[:alpha:].' -]{2,15}$", stripslashes(trim($_POST['title'])))) {
$t = escape_data($_POST['title']);
} else {
$t = FALSE;
$error['Title'] = '<font color="red" size="2">*</font>';
}
// Check for a company.
if (eregi ("^[[:alpha:].' -]{2,30}$", stripslashes(trim($_POST['company'])))) {
$c = escape_data($_POST['company']);
} else {
$c = FALSE;
$error['Company'] = '<font color="red" size="2">*</font>';
}
// Check for an email address.
if (eregi ("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$", stripslashes(trim($_POST['email'])))) {
$e = escape_data($_POST['email']);
} else {
$e = FALSE;
$error['Email'] = '<font color="red" size="2">*</font>';
}
// Check for a username.
if (eregi ("^[[:alnum:]_]{4,24}$", stripslashes(trim($_POST['username'])))) {
$u = escape_data($_POST['username']);
} else {
$u = FALSE;
$error['UserName'] = '<font color="red" size="2">*</font>';
}
if ($fn && $ln && $t && $c && $e && $u) { // If everything's OK.
// Make sure the username is available.
$query = "SELECT user_id FROM users WHERE username='$u'";
$result = @mysql_query ($query);
if (mysql_num_rows($result) == 0) { // Available.
/ Everything has passed both error checks that we have done.
It's time to create the account! /
/ Random Password generator. We'll generate a random password for the
user and encrypt it, email it and then enter it into the db.
/
function makeRandomPassword() {
$totalChar = 7; // number of chars in the password
$salt = "abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789"; // salt to select chars from
srand((double)microtime()*1000000); // start the random generator
$password=""; // set the inital variable
$i = 0;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($salt, $num, 1);
$password = $password . $tmp;
$i++;
}
return $password;
}
$random_password = makeRandomPassword();
$db_password = ($random_password);
// Add the user.
$query = "INSERT INTO users (username, first_name, last_name, title, company, email, password, registration_date) VALUES ('$u', '$fn', '$ln', '$t', '$c', '$e', password('$db_password'), NOW() )";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.
$user_id = mysql_insert_id();
// Let's mail the user!
$subject = "Your registration at printxpress.us";
$message = "Hi $first_name ...Jake here.
Thanks for registering at our website, Print X Press.
To activate your registration, please click here:
[url]http://www.printxpress.us/activate.php?id=[/url]$user_id&code=$db_password
Once activated, you can sign-in with the following information:
Username: $username
Password: $random_password
After signing in you can change the security-generated password to one of your choosing.
Enjoy your visit!
Jake Forest";
mail($email, $subject, $message, "Wrom: WRKJVZCMHVIBGDADRZFSQHYUCDDJBLVLMHAAL
echo 'Registration authentication has been mailed to your email address. Please check it for confirmation.';
}
include ('includes/footer.inc'); // Include the footer.
exit();
} else { // The username is not available.
$error['UserNameNA'] = '<font color="red" size="2">* ...not available</font>';
$error['TopMessage'] = '<font color="red" size="2" face="Arial, Helvetica, sans-serif"><strong>please try again</strong></font>';
}
} else { // If it did not run OK.
// Send a message to the error log, if desired.
$error['TopMessage'] = '<font color="red" size="2" face="Arial, Helvetica, sans-serif"><strong>please try again</strong></font>';
}
mysql_close(); // Close the database connection.
} // End of the main submit1 conditional.
?>
<?php # sign-in
// Include the configuration file for error management and such.
require_once ('includes/config.inc');
if (isset($_POST['submit2'])) { // Check if the form has been submitted.
require_once ('./mysql_connect.php'); // Connect to the database.
if (empty($_POST['username'])) { // Validate the username.
$u = FALSE;
echo '<p><font color="red" size="+1">You forgot to enter your username!</font></p>';
} else {
$u = escape_data($_POST['username']);
}
if (empty($_POST['password'])) { // Validate the password.
$p = FALSE;
echo '<p><font color="red" size="+1">You forgot to enter your password!</font></p>';
} else {
$p = escape_data($_POST['password']);
}
if ($u && $p) { // If everything's OK.
// Query the database.
$query = "SELECT user_id, first_name FROM users WHERE username='$u' AND password=PASSWORD('$p')";
$result = @mysql_query ($query);
$row = mysql_fetch_array ($result, MYSQL_NUM);
if ($row) { // A match was made.
// Start the session, register the values & redirect.
$_SESSION['first_name'] = $row[1];
$_SESSION['user_id'] = $row[0];
ob_end_clean(); // Delete the buffer.
header ("Location: [url]http://[/url]" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
exit();
} else { // No match was made.
echo '<p><font color="red" size="+1">The username and password entered do not match those on file.</font></p>';
}
mysql_close(); // Close the database connection.
} else { // If everything wasn't OK.
echo '<p><font color="red" size="+1">Please try again.</font></p>';
}
} // End of submit2 conditional.