Search code examples
phpcodeigniteremailmysql-workbenchactivation

Account activation, CodeIgniter


I want to create an account activation where after registering, a link would be sent to an administrator (or one) email whereby the admin just has to click that link to activate that account.

I have the registration and login working. I'm using MySQL Workbench and have a "flag" or rather just a field in my accounts table (named user_login) to tell whether the account is enabled or disabled, which is disabled by default after registration.

I am stuck and sending a link through email, I'm not sure where to begin. That link that I want to send would contain a random string and would be sent to the admin, say abc/123/random?stringis=1234. Then the admin would just have to open his email and click on the string and then that specific user's account would be activated. I found this and this but that's just for how to send a link through email.

I don't have an idea on the logic. Do I create a function whereby the link would go directly to the function and from there, it would change the value in my table to enabled or whatever I call it so that the user's account is counted as activated? Do I need to create a new field to match the random generated string then?

Main idea is I'm trying to do like those typical sites whereby a link would be sent to the user to activate the account once he/she clicks it in the email, but this time just to a specific email which is the admin's.

EDIT:

In controller

public function activate_user($activation_code)
{
    $result = $this->home_model->activate($activation_code);

    if($result != FALSE)
    {
        echo "You have activated :".$result[0]->user_id.".";
    }
    else
    {
        echo "Activation failed, something went wrong.";
    }
}

In Model:

public function activate($activation_link)
{
    $this->db->select('*');
    $this->db->from('user_login');
    $this->db->where('activation_link', $activation_link);
    $query = $this->db->get();

    if($query->num_rows() == 1)
    {
        return $query->result();
    }
    else
    {
        return FALSE;
    }
}

Solution

  • First

    Database add two column

    • activation_token{varchar|255}
    • activation_time{datetime}

    After registration Success

    • add some randome has into activation_token(md5 or sha1(up to you))
    • add time if registration using Current timestamp(now())

    Link

    link should be I strongly prefer userid in activation url because it's remove the link duplication. http://sitename.com/activation/{user_id}/{hash}

    in controller

    public function activation($user_id,$hash)
    {
    
      $timeOfexpiration = 3600;
    
      $data = $this->model->get_data($id,$hash);
    
      if(!$data)
      {
        return false
      }
    
        //if user found 
        //check expiration of linke
        //like
    
      if($data['activation_time']+$timeOfexpiration < now())
      {
        return true;
      }
      else
      {
        return false;
      }
    
    }