Tag: activation

Using PHP with Email Activation

Let’s say you have a website with registration. However, you want to have all registered users verify their email address to their account before being able to use all the functions of your site. To do this we can use PHP and simply create an activation code for each user when they register.

Let’s make a simple HTML form that submits a username, password, and email address.

[codesyntax lang=”html4strict” title=”Registration Sample Form”]
<table width=”100%” align=”center” border=”0″ cellpadding=”2″ cellspacing=”0″>
<tr>
<td align=”left”>Username:</td>
<td align=”left”><input type=”text” name=”username” size=”25″ /></td>
</tr>
<tr>
<td align=”left”>Password:</td>
<td align=”left”><input type=”password” name=”password” size=”25″ /></td>
</tr>
<tr>
<td align=”left”>Email Address:</td>
<td align=”left”><input type=”text” name=”email” size=”25″ /></td>
</tr>
<tr>
<td align=”left” colspan=”2″><input type=”submit” name=”submit” value=”Submit” /> <input type=”reset” name=”reset” value=”Reset” /></td>
</tr>
</table>
[/codesyntax]

You would probably want to add a password confirm input or whatever else inputs you want in your registration. This is just for the purpose of submitting the value of the username, password, and email address to our function which will email the user based on the submitted email and include the username and password.

In your registration code you can make an activation code by the following:

[codesyntax lang=”php” title=”Generate Activation Code Function”]
<?php
function generateCode(){
$codelength = 20; // How long you want the activation code to be.
$characters = “abcdefghijklmnopqrstuvwxyz1234567890”; // All accepted characters.
$activatecode = “”;
for($i=0;$i&lt;=$codelength;$i++){
$activatecode .= substr(str_shuffle($characters),0,1);
}
return $activatecode;
}

$userActivationCode = generateCode();
?>
[/codesyntax]

Along with your registration code when you submit the user’s data to your mysql database you would include this in there. We must be connected to a mysql database in order to alter a table (in this case “users”). You can see how to connect to a Mysql database here! You will need to create a column in your users table for both the activation code and their activated status. Like so:

[code lang=”php”]
<?php
$createcolumn = mysql_query(“ALTER TABLE `users` ADD COLUMN activatecode VARCHAR(20)”);
$createcolumn = mysql_query(“ALTER TABLE `users` ADD COLUMN activatestatus INT(5) DEFAULT 0″);
?>
[/code]

The activatestatus column is used to check if their account is activated or not. Now, let’s make it so they have to click this link in their email after they have registered.

Upon registration we will send an email to them with this code in it.

[codesyntax lang=”php” title=”Send Activation Email Function”]
<?php
function sendActivationEmail($email,$username,$password,$actcode){
// Let’s make sure the email address is valid.
if(preg_match(“/^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$/”, $email)){

// Clean up the username and password.
$username = trim(addslashes($username));
$password = trim(addslashes($password));

if($username && $password){
// Now let’s send the email to them!

mail($email,”Welcome to mysite.com!” Thank you for registering! Below are your account details as well as your activation link needed to complete registration.\n\n
Username: $username\n
Password: $password\n\n
Your activation link:\n http://mysite.com/?actcode=$actcode\n\n
Thank you and welcome to the site!”);
// You can edit the details of the email however you wish of course.

return “Your activation email has been sent to the email specified.”;
} else {
return “Please enter a username and password.”;
}
} else {
return “Invalid email address entered.”;
}
}

// To send the email we define it as $sendEmail with the parameters of our submitted email, username, and password.
$sendEmail = sendActivationEmail($_POST[“email”],$_POST[“username”],$_POST[“password”], $userActivationCode);
?>
[/codesyntax]

This $sendEmail contains the returned value from our sendActivationEmail() function. So apon submitting the registration form this function is called, and stores the returned value in this variable. To display the result of this function you would simply echo or print the variable.

Example:

[code]
<?php
// Once the registration form has been submitted and we called our sendActivationEmail() function.
if(!empty($sendEmail)){ echo $sendEmail; }
?>
[/code]

Now, this function uses four variables that are pre-defined. The $actcode comes from the function we created called “generateCode”. The username, password, and email variables are coming from a form the user submits to register.

Okay, now we have sent them their email with the link. So when they click the link, what happens? Well we need to make this code. What we will do is execute a sql query to update their row in the table and set activatestatus = 1.

[codesyntax lang=”php” title=”Index.php Activate User PHP”]
<?php
if(isset($_GET[“actcode”])){

// Clean the activate code.
$activatecode = trim(addslashes($_GET[“actcode”]));

// Check for their row with that specified activate code.
$sql = mysql_query(“SELECT id FROM users WHERE activatecode = “.$activatecode.” AND activatestatus = ‘0’ LIMIT 1″);

if(mysql_num_rows($sql) > 0){
// Code exists and they aren’t active, let’s make them active.
$update = mysql_query(“UPDATE users SET activatestatus = ‘1’ WHERE activatecode = “.$activatecode.””);
echo “Your account has been activated!”;
} else {
// Code not found.
echo “Invalid activation code.”;
}
}
?>
[/codesyntax]

Now that the user’s activate status has been updated to “1” we can check this column when logging the user in. So when the user goes to enter their login details (username and password) we will simply add a “WHERE username = ‘$username’ AND password = ‘$password’ AND activatestatus = ‘1’“. This will ensure no unactivated accounts may login.

And there you have it. Email activation is quite useful. Enjoy.

Filed under: TutorialsTagged with: , ,