View Full Version : Email account activation help needed.

08-20-2011, 12:35 AM
I am building a site that requires users to create an account before they can access other pages. Building the new account creation and login was easy enough. The next thing I want to do is make it so my users have to click an activation link sent to their email to activate their account.

I am able to use PHP to automatically generate an email to the user, but I just don't know how to make an activate.php page that would check the $active value and id from the URL, then set the active field value in the database to 'active'

Here is my code so far:


$active = md5(uniqid(rand(), true));


if(isset($todo) and $todo=="post"){

$status = "OK";

// if userid is less than 3 char then status is not ok
if(!isset($userid) or strlen($userid) <3){
$msg=$msg."User id should be =3 or more than 3 char length<BR>";
$status= "NOTOK";}

if(mysql_num_rows(mysql_query("SELECT userid FROM plus_signup WHERE userid = '$userid'"))){
$msg=$msg."Userid already exists. Please try another one<BR>";
$status= "NOTOK";}

if(mysql_num_rows(mysql_query("SELECT email FROM plus_signup WHERE email = '$email'"))){
$msg=$msg."Email already exists. Please try another one<BR>";
$status= "NOTOK";}

if ( strlen($password) < 3 ){
$msg=$msg."Password must be more than 3 char length<BR>";
$status= "NOTOK";}

if ( $password <> $password2 ){
$msg=$msg."Both passwords are not matching<BR>";
$status= "NOTOK";}

if ( strlen($phone) < 10 ) {
$msg=$msg. "Phone Number must be 10 digits<BR>";
$status= "NOTOK";}

if ($agree<>"yes") {
$msg=$msg."You must agree to terms and conditions<BR>";
$status= "NOTOK";}

echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Retry' onClick='history.go(-1)'>";
if(mysql_query("insert into plus_signup(userid,password,email,name,phone,active) values('$userid','$password','$email','$name','$phone','$active')")){
mail($_POST['email'], 'User Registration', 'Click here to activate your account: http://www.myExample.com/activate.php?u=' . mysql_insert_id() . '&c=' . $active);
echo "<font face='Verdana' size='2' color=green>Welcome, You have successfully signed up. <br><br>In order to view content, you must first follow the activation link we sent to your email.<br></font>";
echo "<font face='Verdana' size='2' color=red>$msg</font><br><input type='button' value='Login' onClick='history.go(-2)'>";}
else{ echo "Database Problem, please contact Site admin";

//echo mysql_error();



08-20-2011, 02:14 AM
Use this tutorial: http://www.learnphponline.com/scripts/email-activation-for-php-forms

First create a database columns with these like information.

`status` varchar(20) NOT NULL,
`activationkey` varchar(100) NOT NULL,
UNIQUE KEY `activationkey` (`activationkey`)

The status would be 1 or 0, 1 for active 0 for inactive/not activated.

$queryString = $_SERVER['QUERY_STRING'];

$query = "SELECT * FROM users";

$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){

if ($queryString == $row["activationkey"]){

echo "Congratulations!" . $row["username"] . " is now the proud new owner of a YOURWEBSITE.com account.";

$sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";

if (!mysql_query($sql))


die('Error: ' . mysql_error());




You can use the GET function.