Page 1 of 1

Generate Code For Activation Link [SOLVED]

Posted: Wed Feb 27, 2008 11:43 am
by the9ulaire
After a user registers on my site, I want them to be sent an email with an activation link. With the link, how should I go about creating a unique value for them so when they click it, it takes them to a page to activate their link.

I hope I'm clear on what I'm asking.

Thanks all!
Luke

Re: Generate Code For Activation Link

Posted: Wed Feb 27, 2008 11:58 am
by Christopher

Re: Generate Code For Activation Link

Posted: Wed Feb 27, 2008 12:07 pm
by Monotoko
I hope i can explain this well, with you understanding, if not, please say and i will give some examples.

The way i do it is:
when a user registers, set a database value (i use 'activated') to 0 automaticaly
while placing stuff like the username and password in the DB, use php_rand eg

Code: Select all

 
<?php
$activate_num = rand(0,9999999999999999999)
?>
stick as many nines as you like in there, make the number as big or as small as you want

save it in the DB, like so:

Code: Select all

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("my_db", $con);mysql_query("INSERT INTO members (act_code) 
VALUES ('$activate')"); 
?>
Finaly, we need to email the user with the code and his username, and his password if you want to send it

Code: Select all

<?php
$subject = "Your New Account!";
$message = "Hello! welcome to blah blah, your username is $user and your validation code is $activate_num";
$from = "accountregthingy@example.com";
$headers = "From: $from";
//$email is not defined here, you shall have to do that yourself, upon the registration stick there email into the variable $email
mail($email,$subject,$message,$headers);
echo "Mail Sent.";
?> 
 
Now its in there, we need to set up a page, this works kind of like the login page, except it will check that the username+validation code are right rather than the username+password. If its right, set the active row to 1 on that user, the user is now activated.

Re: Generate Code For Activation Link

Posted: Wed Feb 27, 2008 4:24 pm
by the9ulaire
Thanks guys!