By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,441 Members | 998 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,441 IT Pros & Developers. It's quick & easy.

Activating users using email in php

P: 2
Please help me to get a php code that activates users upon registration by sending the activation codes to their specified emails.

@Rabbit

Expand|Select|Wrap|Line Numbers
  1. $host  = $_SERVER['HTTP_HOST'];
  2. $host_upper = strtoupper($host);
  3. $login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
  4. $path   = rtrim($login_path, '/\\');
  5.  
  6. $message = 
  7. "Thank you for registering with us. Your account has been activated...
  8.  
  9. *****LOGIN LINK*****\n
  10. http://$host$path/login.php
  11.  
  12. Thank You
  13.  
  14. Administrator
  15. $host_upper
  16. ______________________________________________________
  17. THIS IS AN AUTOMATED RESPONSE. 
  18. ***DO NOT RESPOND TO THIS EMAIL****
  19. ";
  20.  
  21.     @mail($to_email, "User Activation", $message,
  22.     "From: \"Member Registration\" <auto-reply@$host>\r\n" .
  23.      "X-Mailer: PHP/" . phpversion());
  24.  
  25.  
  26.  echo "Active";
  27.  
  28.  
  29. }
Mar 21 '12 #1
Share this Question
Share on Google+
2 Replies


helimeef
P: 77
What exactly are you trying to accomplish? Are you sending an activation code after they fill out a registration form? What I would do is this:
  1. Generate a random number for each user and store it in a database.
  2. Send them a hash of this random number in your email (retrieve it from the database, then sha1() or md5() it).
  3. When they click on the link/enter the activation code, retrieve the number from the database, hash it, and compare it to the hash they have. If they match, your user is legitimate.

Example:
Expand|Select|Wrap|Line Numbers
  1. function registerUser() {
    //... whatever code you already have here
    $activationCode = rand();
    //... insert $activationCode into the database alongside other user info
    }
  2.  
  3. //... meanwhile, the user clicks on a link in the email with the hash in a GET variable
  4.  
  5. $submittedHash = $_GET['ac'];
  6.  
  7. $storedCode = /* Put whatever code here to retrieve the number you stored in the database earlier */;
  8.  
  9. if ($submittedHash == md5($storedCode)) 
    loginSuccessful($submittedHash); // or whatever
Mar 26 '12 #2

helimeef
P: 77
You can also check out this tutorial for even more comprehensive help.
Mar 26 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.