Home » Php » php – How can I do member registration confirmation?

php – How can I do member registration confirmation?

Posted by: admin July 12, 2020 Leave a comment


How can I register with CakePHP ?

After registering , it will send email for confirmation.

If I click the link for confirmation then the account will be confirmed.

How can I do this?

Is there any function with Auth to do that?

Or do I have to send mail manually to confirm registration ?

If I have to send email manually to confirm registration, then how can I generate the registration token, and how can I set time to be a valid token ?

Can anyone show an example of this?

How to&Answers:

Check the source of the Cake Development Corporations users plugin, its available for CakepPHP 1.3 and 2.0. https://github.com/cakedc/users It already does everything – in a proper MVC and CakePHP way – that you request. Simply use the plugin or take some of the code.


user table:

  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  `password` varchar(255) COLLATE utf8_persian_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_persian_ci NOT NULL,
  `created` datetime NOT NULL,
  `status` tinyint(1) NOT NULL,
  `activation_code` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)

when user registered, u can set a unique string (sample: md5(time()) or any thing…) in activation_code field.
now send email like this url to user:


now you have to check in your action that this activation_code is in user table or not.

and if is then is that status = disable or not….


You can easily generate a hashcode like a token in PHP and validate it’s duration by a TimeStamp. For email just use the Email component like this. If you want to use Auth Component, be sure that your form give you the correct hash for password.

function register() {
    $error = false;
    $error_captcha = null;
        App::import('Component', 'Converter'); 
            $error = TRUE;
            $error = TRUE;
        $captcha_respuesta = recaptcha_check_answer ($this->captcha_privatekey,
        if ($captcha_respuesta->is_valid && !$error) {
        $this->data['User']['coderegistration'] = $this->generate->getUserCode();
        $this->data['User']['displayname'] = $this->data['User']['firstname'] . " " . $this->data['User']['lastname'];
        $this->data['User']['seotitle'] =  $this->converter->seotitle($this->data['User']['username']);
        $this->data['User']['password'] =  md5($this->data['User']['username'].$this->data['User']['password']);
        $this->User->id = NULL;
            send email notification
            $email = $this->data['User']['email'];              
            $content = sprintf('<a href="%s/%s">here</div>', $this->url, $this->data['User']['coderegistration']);
            $this->email->to = $email; 
            $this->email->subject = 'you have been registered, please confirm'; 
            $this->email->replyTo = '[email protected]'; 
            $this->email->from = "name <[email protected]>";                
    $this->email->template = 'notification'; 
            $this->email->sendAs = 'html';
        $this->set('value', $content);  
                // OK                   
            trigger_error("error Mail");

           $error_captcha = $captcha_respuesta->error;

    $this->layout = "home";
    $this->set('backurl', '/');



to send the email load the Email component also . The register function given already is good and you should go with that.

basically concept is to add user, then create a token (With timestamp ro whatever) and save this to a database, then send an email with a link to that token.

Then when the user clicks the link to the token you set user = active and they are now registerd and can login.

A good tip for your Auth therefore is add a “scope” (check the cakephp docs for 1.3) to Auth. Make this scope the condtion that active = 1. So that way they will need to have confirmed from the email link, and can never login until this is done. Easy!