• 15th February 2009 - By admin

    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.

    <form action="register.php" method="post">
    Username: <input type="text" name="username" size="20" maxlength="25" /><br/>
    Password: <input type="password" name="password" size="20" maxlength="30" /><br/>
    Email: <input type="text" name="email" size="30" maxlength="50" /><br/>
    <input type="submit" value="Submit" name="submit" />
    </form>

    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:

    <?php

    function generateCode(){
    $codelength = 20; // How long you want the activation code to be.
    $characters = "abcdefghijklmnopqrstuvwxyz1234567890"; // All accepted characters.
    $activatecode = "";
    for($i=0;$i<=$codelength;$i++){
    $activatecode .= substr(str_shuffle($characters),0,1);
    }
    return $activatecode;
    }

    $userActivationCode = generateCode();

    ?>

    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:

    $createcolumn = mysql_query("ALTER TABLE `users` ADD COLUMN activatecode VARCHAR(20)");
    $createcolumn = mysql_query("ALTER TABLE `users` ADD COLUMN activatestatus INT(5) DEFAULT 0");

    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.

    <?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,"
    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);

    ?>

    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.

    <?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.";
    }
    }
    ?>

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

  • 26 Responses to “Using PHP with Email Activation”

    • Pett on March 2, 2009

      Greatings,
      Interesting, I`ll quote it on my site later.

      Have a nice day
      Pett

    • Eremeeff on March 17, 2009

      Hi,
      bgallz.org to GoogleReader!

      Thanks
      Eremeeff

    • Rufor on March 23, 2009

      Hi,
      bgallz.org – da best. Keep it going!

      Thanks
      Rufor

    • Marlon Asen on June 6, 2010

      Now you’re making me jealous .

    • Nick Crill on June 13, 2010

      thanks to your ideas , i¡¯d adore to adhere to your weblog as usually as i can.possess a good day

    • listen to spanish music on June 14, 2010

      Yes! thought your post is a time saver!

    • Rodney Vanderschoot on June 19, 2010

      Needless to say, what a excellent site and enlightening posts, I’ll include backlink – bookmark this website? Regards, Reader.

    • Maribeth Minzel on June 19, 2010

      Great site. Beneficial data

    • Dayna Lochan on June 19, 2010

      Intriguing insight, stunned I certainly not thought of that by myself

    • Samuel Gambaiani on June 20, 2010

      Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader

    • Nigel Junghans on June 23, 2010

      Howdy, your site is on air in the radio! Good job mate. Your posts are truly great and bookmarked. Regards

    • youtube marketing on June 29, 2010

      I love your website! did you create this yourself or did you outsource it? Im looking for a blog design thats similar so thats the only reason I’m asking. Either way keep up the nice work I was impressed with your content really..

    • youtube marketing on June 29, 2010

      I love your blog! did you create this yourself or did you outsource it? Im looking for a blog design thats similar so thats the only reason I’m asking. Either way keep up the nice work I was impressed with your content really..

    • Louetta Orsino on July 1, 2010

      Come on dude, these facts* and proof* i mean who’s posting* lol :P

    • Home Insurance on July 10, 2010

      Perceptive word, valuable and first-rate map, as deal satisfactory equipment with right ideas and concepts, lots of well-known knowledge and inspiration, both of which we all constraint, thanks for all the eagerness to offer such helpful news here.

    • breast enhancement on July 10, 2010

      Hey, I have been searching for answers to my questions of writers, and after looking the search engines, I ended up your site, it isexcellent writing. Too bad I took all these days to get to read this article. Saved you in my bookmarks already. Will come back pretty soon. Keep writing and follower count will go up.

    • Jenae Balint on July 12, 2010

      Wow!, this was a top quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I keep putting it off and never seem to get something done

    • webdesign on July 15, 2010

      I have read few of articles on your blog and could say it was really interesting, thanks for sharing that.

    • linki sponsorowane google on July 19, 2010

      Sorry my english, but this is great site, I wil add to my favorites.

    • Brishti on August 4, 2010

      It’s really a helpful code.For this particular code i surfed many sites,but this one is the best and really helpful.

    • Nike Air Jordan Shoes on August 4, 2010

      Are there other sites that you can recommend me?Thank you very much!

    • Coloring Pages on August 5, 2010

      Good read. Where is your contact details though?

    • Free Avatars on August 13, 2010

      I liked reading this, found you through Google.

    • Christen Suon on August 14, 2010

      I recently came across your blog and have been reading along. I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.

    • Malka Forsman on August 19, 2010

      So Great! I need some infos in this post for my rapport de stage. Can i have your contact please? I need your permission to quote it :D . Anyway, That’s great job. Keep going.

    • Aline Ramesh on August 20, 2010

      So Great! I need some infos in this post for my rapport de stage. Can i have your contact please? I need your permission to quote it :D . Anyway, That’s great job. Keep going.

    Leave a Reply

    Powered by WP Hashcash


bgallz.org is for training purposes only. Its content is to be used at the risk of the user. We do not guaruntee its accuracy.