943,899 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 658
  • PHP RSS
Aug 27th, 2009
0

User activation help

Expand Post »
In order for my users to log in they have to activate their account through email. The activation works fine. The trouble I am having is displaying a message at login if the user has not activated. Here is the activation script:

php Syntax (Toggle Plain Text)
  1. <?php
  2. require "connect.php";
  3.  
  4. if(isset($_GET['u'])){
  5. //make sure that 'u' is numeric
  6. if(is_numeric($_GET['u'])){
  7. $u=$_GET['u'];
  8. }else{
  9. $u=0;
  10. }
  11. }
  12.  
  13. if(isset($_GET['a_code'])){
  14. $code=$_GET['a_code'];
  15. }else{
  16. $code=0;
  17. }
  18.  
  19. //Check to see if the received values are correct
  20. $sql = "SELECT * FROM members WHERE uid = '".$u."' AND actcode = '".md5(0)."'";
  21. if(($u > 0) && (strlen($code)) == 32){
  22.  
  23. //now activate the user
  24. $sql="UPDATE members SET actcode ='".md5(1)."' WHERE uid = '".$u."'";
  25.  
  26. $res = mysql_query($sql) or die(mysql_error());
  27.  
  28. if(mysql_affected_rows() == 1){
  29.  
  30. //update successful
  31. echo ('Your account is now activated. You may proceed and log in.<br/><br /><a href="http://www.eternalhour.com/login.php">Login Page</a>');
  32. }else{
  33. echo ('Your account could not be activated. Please try again or contact the site admin.<br/><br/><a href="http://www.eternalhour.com/registration_form.php">Registration Page</a>');
  34. }
  35. }
  36. ?>
I have tried setting up a query then using a $_GET to compare the codes.
But I am imagining the code for the login script will look something like:
php Syntax (Toggle Plain Text)
  1. if(isset($_GET['u'])){
  2. $u=$_GET['u'];
  3. }
  4.  
  5. $sql = "SELECT * FROM members WHERE uid = '".$u."' AND actcode = '".md5."'";
  6. if($row['actcode']) == (md5(1)){
  7.  
  8. }else{
  9. $msg = "You need to activate your account before logging in.";
  10. }
But what I don't understand, is how to verify that the string == md5(1). Could someone help me please?
Last edited by Tekkno; Aug 27th, 2009 at 3:56 pm.
Similar Threads
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009
Aug 27th, 2009
0

Re: User activation help

When a user registers you must provide a link to the verification page with two get variables U & AID (activation ID), you must also store these details in the table.

When it verifies a user then remove all values from the AID field thus leaving it blank.

Then when it comes to logging them in then you only need to check if the field value is true, if it is then they haven't vrified there account if it isn't then they have and you can create the cookie.
Reputation Points: 31
Solved Threads: 27
Unverified User
Josh Connerty is offline Offline
342 posts
since Apr 2009
Aug 28th, 2009
0

Re: User activation help

I have an activation script in place, which works just fine. I'd rather not change the script I have, because everything works perfectly as it is. The users can log in, if they have activated. I am just looking for a way to find out if they have activated from my login script, so I can notify them that they need to activate in order to log in.
Last edited by Tekkno; Aug 28th, 2009 at 3:13 am.
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009
Aug 28th, 2009
0

Re: User activation help

You aren't understanding how an activation script works. As of right now yours makes no sense. The values you are passing via get are not even being used in your queries.

The point of an activation script is to send a unique random number that is hard to guess (md5 of 1 will be the same every time, thats pointless). This helps make sure that the link was sent to their email address only and no one elses. The activation script needs to get the values from the url and use them in the database to identify the user that registered and mark them as 'active'.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Aug 28th, 2009
0

Re: User activation help

Well forgive me for not being an expert, but why do you think I am here asking? You may know PHP better than I do, but your people skills really need some work. My script is obviously incorrect, thanks for the criticism.
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009
Aug 28th, 2009
0

Re: User activation help

On forums I tend to say things without the other persons feelings in mind (probably this is is because its not a face to face conversation). I didn't realize how rude that sounded until after I read again. For that, I apologize.

To make up for it, I will tell you exactly what to do.

On your registration page you need to create the link, email it, and save the random string in the database. I usually use a column named 'active' for this.
php Syntax (Toggle Plain Text)
  1. //this is after form validates and you are about to insert the data into the database
  2. function randString( $length ) {
  3. $array = array("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z","B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9");
  4. $i = 0;
  5. $code = '';
  6. while ( $i < $length ) {
  7. $rand = rand( 0,( count( $array ) - 1 ) );
  8. $code .= $array[$rand];
  9. $i++;
  10. }
  11. return $code;
  12. }
  13. $code = randString(50);
  14. mysql_query("INSERT INTO `members` (....column names....,'active') VALUES (....values here....,'{$code}')") or die(mysql_error()); //shows how you insert the code
  15. $message = "Welcome to something.com,\n\nTo activate your account click the link below:\n\nhttp://www.something.com/activate.php?id=" . mysql_insert_id() . "&code={$code}\n\nBest regards,\n\nAdmin\nSomething.com (info@something.com)";
  16. mail('email of user from form','Email Confirmation',$message,"From: Something.net<no-reply@something.net>"); //sends the activation email with the id of the user and activation code.

On activate.php, we use the id and the code the id the user and set them to active.
php Syntax (Toggle Plain Text)
  1. if ( isset( $_GET['id'],$_GET['code'] ) ) {
  2. $id = (int) $_GET['id'];
  3. $code = mysql_real_escape_string( $_GET['code'] );
  4. $query = mysql_query("SELECT `active` FROM `members` WHERE `id` = {$id}");
  5. if ( mysql_num_rows( $query ) == 1 ) {
  6. list( $active ) = mysql_fetch_rows( $query );
  7. if ( $active == 1 ) {
  8. die('User account has already been activated');
  9. }
  10. elseif ( $active == $code ) {
  11. mysql_query("UPDATE `members` SET `active` = 1 WHERE `id` = {$id}"); //sets the member to active.
  12. }
  13. else {
  14. die('Invalid activation code');
  15. }
  16. }
  17. else {
  18. die('User not found!');
  19. }
  20. }
  21. else {
  22. die('Malformed URL');
  23. }

In your login script all you need to do is add ' AND `active` = 1' in your query.
Last edited by kkeith29; Aug 28th, 2009 at 5:26 am.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Aug 28th, 2009
0

Re: User activation help

Apology accepted, it was obviously not intentional. Thanks for this code keith, but this brings me back to my original question. With your script, how do I notify the user at login that they need to activate their account in order to log in?

php Syntax (Toggle Plain Text)
  1. if ( isset( $_GET['id'],$_GET['code'] ) ) {
  2. $id = (int) $_GET['id'];
  3. $code = mysql_real_escape_string( $_GET['code'] );
  4. $query = mysql_query("SELECT `active` FROM `members` WHERE `id` = {$id}");
  5.  
  6. if($row['active']) == 1){
  7.  
  8. }else{
  9.  
  10. $msg = "You need to activate your account before logging in.";
  11.  
  12. }
Maybe?
Last edited by Tekkno; Aug 28th, 2009 at 4:52 pm.
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009
Aug 28th, 2009
1

Re: User activation help

My script goes on its own page called activate.php.

In your login script you should get the user via a username/email which you have probably already done.
ex.
PHP Syntax (Toggle Plain Text)
  1. $username = mysql_real_escape_string( $_POST['user'] ); //username from form
  2. $query = mysql_query("SELECT `password`,`active` FROM `table_name` WHERE `username` = '{$user}' LIMIT 1");
  3. if ( mysql_num_rows( $query ) == 0 ) {
  4. $msg = 'Username and/or Password incorrect'; //never be specific
  5. }
  6. else {
  7. list( $password,$active ) = mysql_fetch_row( $query );
  8. if ( $active !== 1 ) {
  9. $msg = 'You need to activate your account before logging in.';
  10. }
  11. else {
  12. //check the password and set login session here. Your passwords should be hashed.
  13. }
  14. }

Make sure your passwords are hashed as well.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Aug 29th, 2009
0

Re: User activation help

Unfortunately Keith, I could not get my activation working with the script you posted. However, after changing the way my activation works and adjusting the login script, I was able to get everything working properly. Thanks for your time and effort.
Reputation Points: 12
Solved Threads: 7
Junior Poster
Tekkno is offline Offline
134 posts
since Apr 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: PHP HTTP Screen-Scraping Class with Caching
Next Thread in PHP Forum Timeline: Installing php libraries on linux





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC