Activating users using email in php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KevinKiambe
    New Member
    • Mar 2012
    • 2

    Activating users using email in php

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

    Originally posted by Rabbit
    What have you tried?

    Code:
    $host  = $_SERVER['HTTP_HOST'];
    $host_upper = strtoupper($host);
    $login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
    $path   = rtrim($login_path, '/\\');
    
    $message = 
    "Thank you for registering with us. Your account has been activated...
    
    *****LOGIN LINK*****\n
    http://$host$path/login.php
    
    Thank You
    
    Administrator
    $host_upper
    ______________________________________________________
    THIS IS AN AUTOMATED RESPONSE. 
    ***DO NOT RESPOND TO THIS EMAIL****
    ";
    
    	@mail($to_email, "User Activation", $message,
        "From: \"Member Registration\" <auto-reply@$host>\r\n" .
         "X-Mailer: PHP/" . phpversion());
    
    
     echo "Active";
    
    
    }
    Last edited by Niheel; Mar 22 '12, 07:12 PM. Reason: please always post code as part of question
  • helimeef
    New Member
    • Sep 2007
    • 77

    #2
    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:
    Code:
    function registerUser() {[INDENT]//... whatever code you already have here[/INDENT][INDENT]$activationCode = rand();[/INDENT][INDENT]//... insert $activationCode into the database alongside other user info[/INDENT]
    }
    
    //... meanwhile, the user clicks on a link in the email with the hash in a GET variable
    
    $submittedHash = $_GET['ac'];
    
    $storedCode = /* Put whatever code here to retrieve the number you stored in the database earlier */;
    
    if ($submittedHash == md5($storedCode)) [INDENT]loginSuccessful($submittedHash); // or whatever[/INDENT]

    Comment

    • helimeef
      New Member
      • Sep 2007
      • 77

      #3
      You can also check out this tutorial for even more comprehensive help.

      Comment

      Working...