We are going to build a nice PHP sign-up script where a user can create an account to gain access to the "members only section" of a website.
After the user creates their account, the account will then be locked until the user clicks a verification link that they'll receive in their email inbox.
// REGISTER USER
if (isset($_POST['register_btn'])) {
// receive all input values from the form
$firstname = esc($_POST['firstname']);
$lastname = esc($_POST['lastname']);
$genders = esc($_POST["gender"]);
$username = esc($_POST['username']);
$email = esc($_POST['email']);
$password_1 = esc($_POST['password_1']);
$password_2 = esc($_POST['password_2']);
// form validation: ensure that the form is correctly filled
if (empty($firstname)) {
array_push($errors, "Uhmm...You missed your First Name");
}
if (empty($lastname)) {
array_push($errors, "Uhmm...You missed your Last Name");
}
if (empty($username)) {
array_push($errors, "Uhmm...We gonna need your username");
}
if (empty($email)) {
array_push($errors, "Oops.. Email is missing");
}
if (empty($password_1)) {
array_push($errors, "uh-oh you forgot the password");
}
if ($password_1 != $password_2) {
array_push($errors, "The two passwords do not match");
}
// Ensure that no user is registered twice.
// the email and usernames should be unique
$user_check_query = "SELECT * FROM users WHERE username='$username' OR email='$email' LIMIT 1";
$result = mysqli_query($conn, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user) { // if user exists
if ($user['username'] === $username) {
array_push($errors, "Username already exists");
}
if ($user['email'] === $email) {
array_push($errors, "Email already exists");
}
}
// register user if there are no errors in the form
if (count($errors) == 0) {
$password = md5($password_1); //encrypt the password before saving in the database
$vkey = md5(time().$username);
$cenvertedTime = date('Y-m-d', strtotime(' + 5 days'));
$query = "INSERT INTO users (firstname, lastname, gender, username, email, password, vkey, token_expire, created_at, updated_at)
VALUES('$firstname', '$lastname', '$genders', '$username', '$email', '$password', '$vkey', '$cenvertedTime', now(), now())
";
$resultset = mysqli_query($conn, $query);
$inserted_id = mysqli_insert_id($conn);
$user_check = "SELECT token_expire FROM users WHERE id='$inserted_id' LIMIT 1";
$result = mysqli_query($conn, $user_check);
$date = mysqli_fetch_assoc($result);
$created_at = $date['token_expire'];
$created_at = date("F j, Y ", strtotime($created_at));
if ($resultset) {
$auto = date(Y);
$to = $email;
$sent_date = date("F j, Y ");
$subject = 'Please confirm your email address';
$message = " < !DOCTYPE html > < html lang = 'en' > < head > < meta charset = 'UTF-8' >
< meta charset = 'iso-8859-1' >
< meta name = 'viewport'
content = 'width=device-width, initial-scale=1.0' >
< link rel = 'shortcut icon'
href = ' .BASE_URL . '
assets / image / slide1.png ' type='
image / x - icon ' /> < link rel = '
stylesheet '
href = ' .BASE_URL . '
password - recovery / main.css '> < title > Reset Your Password | G3 tech < /title> < /head> < body > < div style = '
background - color: rgba(44, 62, 80, 0.8);
max - width: 100 % ;
height: 80 px;
'> < a href = '
https: //www.g3techdesign.com/channel.php' >
< img src = 'https://www.g3techdesign.com/assets/image/mobileg3tech.png'
max - width = '100%'
height = '80px' >
< /a> < /div > < div style = '
max - width: 100 % ;
min - height: 100 % ;
font - size: 20 px;
border: 2 px solid rgba(44, 62, 80, 0.8);
color: #a4c9fe!important;
background - color: black!important;
padding: 1 px 10 px 10 px 10 px;
'> < p > Dear < b > < u > ".$firstname." < /u> <u>".$lastname."</u > < /b>,</p > < p > Welcome to G3Tech, and we would like to take an opportunity to thank you
for taking the time to create an online account with us!
We need you to verify your identity by the following simple steps: < p / >
< p > Step 1: Clik the button below to activate your account. < /p> < center > < a href = \"https:/ / www.g3techdesign.com / login.php ? vkey = ".$vkey.
"\"
style = 'background: #EDBD11;
color: white;
cursor: pointer;
padding: 10 px 20 px;
margin: 8 px 0;
border: none;
opacity: 0.9;
'>Activate Account</a></center> < p > If the above button will not work properly you may copy the following URL and paste in your favorite browser.Here is a link: < br / >
https: //www.g3techdesign.com/login.php?vkey=" . $vkey . "</p>
< p > Step 2: Login with your email / user name and password. < /p>
< p > Step 3: Access your account and resource you would like. < /p>
< p > Upon completing these steps, you 'll have full access to your account portal and customize you profile. Thank you for your interest in G3Tech!</p>
< p > < font color = 'red' > Note: - < /font> This verification link will be expire on ". $created_at .", then the account you created will deleted automatically. If you believe you have received this email in error, please contact us at info@g3techdesign.com </p >
< p > Regards, < br / >
All < a href = 'https://www.g3techdesign.com' > G3Tech < /a> Teams</p >
< /div> < p style = 'color:gray;' > This email was sent to: ". $to ."
on ". $sent_date ." < /p> < div style = '
position: relative;
background - color: black; /*#272727;*/
color: orange;
text - align: left;
bottom: -10 px!important;
padding: 10 px - 10 px 5 px 10 px;
z - index: 1;
font - size: 14 px;
width: 100 % ;
height: 100 px;
'> < h3 > < b > < font color = orange > Copyright & copy;
". $auto ."
All Rights Reserved. < /font></b > < /h3> <a href='https:/ / www.g3techdesign.com / termandprivacy.php ' style='
float: right;
'>Terms and Privacy</a> </div> < /body> < /html>
";
$headers = "MIME-Version: 1.0".
"\r\n";
$headers. = "Content-type:text/html;charset=UTF-8".
"\r\n";
$headers. = 'From: Do-Not-Reply noreply@g3techdesign.com'.
"\r\n";
mail($to, $subject, $message, $headers);
$_SESSION['message'] = "<i class='fas fa-check-circle'></i> Thank you for signing up in to our system. We have sent a verification email to the address you provide.<br/><b>".$to.
"</b> is the email address you provide to us to activate your account.";
// redirect to public area
header('location: '.BASE_URL.
'thankyou.php?email='.$email);
exit(0);
} else {
$_SESSION['message'] = "Something want wrong.";
// redirect to public area
header('location: '.BASE_URL.
'signup.php');
exit(0);
}
}
}
We have made the source code available for download. You will be able to access it once you have logged in. Please log in to download the source code.