How to activate a user’s email registration?

Asked

Viewed 1,080 times

2

Well, when registering the user has its ACTIVE set to 0, how to send an email with a link so that when clicking this link the user updates the asset to 1.

  • 1

    What’s the mistake? What’s the problem? Try to describe the situation better.

  • Cara Voce didn’t post many details of the problems but I think I know what’s going on. In mysql functions, like mysql_query, it was optional to send or not your connection variable. Already in the mysqli functions, you have to send the connection variable if not from Warning, try to do like this: mysqi_query($sql, $con);

  • Check whether the variable link has the connection, tbm put the part of the code where you call mysqli_insert_id

  • The link variable has the connection.

  • And the code of the Insert?

  • That must be the error then because I have nothing from Insert other than that line, but that’s how it worked before with mysql.

  • If you were able to solve the problem create an answer and explain what you had to do, when you have a problem other than the original question you better add a new one as a reference.

  • All right, I’ll fix it.

Show 3 more comments

1 answer

2


I Gero a token and saved it on my user, then I search in the database the user that contains this token, so you recover the user without much work. To generate the token I use the following methods

public function onRegistrationSuccess($user)
{
    $user->setEnabled(false);
    if (null === $user->getConfirmationToken()) {
        $user->setConfirmationToken($this->generateToken());
    }

    $this->sendConfirmationEmailMessage($user);
}

public function generateToken()
{
    return rtrim(strtr(base64_encode($this->getRandomNumber()), '+/', '-_'), '=');
}

private function getRandomNumber()
{
    return hash('sha256', uniqid(mt_rand(), true), true);
}

You can use openssl to change the randomNumber method to this one

private function getRandomNumber()
{
    $nbBytes = 32;

    $bytes = openssl_random_pseudo_bytes($nbBytes, $strong);

    if (false !== $bytes && true === $strong) {
        return $bytes;
    }

    throw new \Exception('OpenSSL não produziu um número aleatório seguro.');

}
  • I do it on the page? $token = generateToken(); and $Numero_aleatorio =getRandomNumber();

  • But then I do the same process of my previous code to send it by email as link?

  • perform the record you generate the token saved it to the user (in a column in the user table) and then send this token to the user’s email as a link (example.com?token=d8s9dad8as). When the user clicks the link he will be taken to your site, recover it from the bank, searching for the user who has the same token and activate it

  • @Iwannaknow I added another method to explain better

  • Before Exception is a "same"?

  • I got it!!! But with the first getRamdomNumber function (do you have a problem using it?). Does this second one have PDO? You’re making a mistake because of public/private and this.

  • 1

    Sorry, I didn’t take the scope of the method, is that I use this in an OO approach, can remove them. About "" is to pick up the object in a global namespace

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.