User activation help

Reply

Join Date: Apr 2009
Posts: 64
Reputation: Tekkno is an unknown quantity at this point 
Solved Threads: 1
Tekkno Tekkno is offline Offline
Junior Poster in Training

User activation help

 
0
  #1
Aug 27th, 2009
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:

  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 340
Reputation: Josh Connerty is an unknown quantity at this point 
Solved Threads: 26
Josh Connerty's Avatar
Josh Connerty Josh Connerty is offline Offline
Posting Whiz

Re: User activation help

 
0
  #2
Aug 27th, 2009
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.
Posts should be like mini-skirts, long enough to cover enough, but not too long that you cover too much.

My Liveperson: http://liveperson.com/josh-connerty/
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 64
Reputation: Tekkno is an unknown quantity at this point 
Solved Threads: 1
Tekkno Tekkno is offline Offline
Junior Poster in Training

Re: User activation help

 
0
  #3
Aug 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: User activation help

 
0
  #4
Aug 28th, 2009
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'.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 64
Reputation: Tekkno is an unknown quantity at this point 
Solved Threads: 1
Tekkno Tekkno is offline Offline
Junior Poster in Training

Re: User activation help

 
0
  #5
Aug 28th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: User activation help

 
0
  #6
Aug 28th, 2009
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.
  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.
  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.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 64
Reputation: Tekkno is an unknown quantity at this point 
Solved Threads: 1
Tekkno Tekkno is offline Offline
Junior Poster in Training

Re: User activation help

 
0
  #7
Aug 28th, 2009
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?

  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: User activation help

 
1
  #8
Aug 28th, 2009
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.
  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.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 64
Reputation: Tekkno is an unknown quantity at this point 
Solved Threads: 1
Tekkno Tekkno is offline Offline
Junior Poster in Training

Re: User activation help

 
0
  #9
Aug 29th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC