Generate Code For Activation Link [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Generate Code For Activation Link [SOLVED]

Post 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
Last edited by the9ulaire on Fri Feb 29, 2008 2:57 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Generate Code For Activation Link

Post by Christopher »

(#10850)
Monotoko
Forum Commoner
Posts: 64
Joined: Fri Oct 26, 2007 4:24 pm

Re: Generate Code For Activation Link

Post 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.
the9ulaire
Forum Commoner
Posts: 74
Joined: Mon Jun 11, 2007 11:31 am

Re: Generate Code For Activation Link

Post by the9ulaire »

Thanks guys!
Post Reply