User Registration with Email Verification using PHP and Mysql


I had receive a tutorial request from one of my blog follower to make or demonstrate User Registration with Email Verification using PHP and Mysql. So the flow is quite simple, user registers to a site, and email is sent to their email address for verification. User then click a link in the email message which redirect him back to the site where it’s account gets approved. I have used php-mailer library so you can send mail via your google account from your local server itself.

View Demo

Create a database with user table

The first thing you have to do is to create your database and table where the records can be stored. Use the sql script below.

Setup a form with four fields username, email, password and confirm password. just to be extra sure of the password.

Validation and user registration code with PHP and Mysql

I am skipping the client side validation with javascript, it is there in the demo and the download scripts. When the user submits the form it’s a two step process. First we check if any existing email id is present in the database, if yes we don’t proceed further, if no then we insert the values in the database and send an email to the user. The user status will be pending until user verify their email. See the code to insert the user record below.

Sending mails using your Google account from your local/online server using phpmailer

For sending mail to user you can use php mail function. But that doesn’t work most of the time in your local server. There’s an excellent plugin I use in most of my project “PHPMAILER“. You can send good looking HTML email with your Gmail or any other mail account via SMTP. All you have to do it so set up your email id, password and host and port name.

Approving user registration process from email

The final part is approving the user. When you are sending email to the user, add the user ID that you recieve when you insert the record in the database. So when user hit the link it will be redirected back to your site with the user ID. You can then use that user ID and update the database.

View Demo


About Author

Entrepreneur & Dreamer

I am a passionate Software Professional, love to learn and share my knowledge with others.

Software is the hardware of my life.


  1. Nitish Kasar on

    good Script bro, especially liked the validation and prepared SQL methods thanks for sharing …. Keep it up

  2. thanx for ths script
    but it shows following error

    Fatal error: Undefined class constant ‘MYSQL_ATTR_INIT_COMMAND’ in C:\wamp\www\email-verification-php-mysql\config.php on line 22

      • Hi Shahrukh

        i got the error SMTP error:Could not authenticate
        i will give my credentials like username, password and port number = 465 but it’s not working can you please help

        • The requested URL /email-verification/activate.php was not found on this server.
          hello, i got the error

          • Yashwanth Reddy Guduru on

            This could be a reason when ssl is implemented in php, change the port number to 587 and the connection to TLS.
            Some times even smtp of gmail gives you error, so change the gmail settings to allow less secure apps if you are trying from any local servers like wamp or xamp.

            These steps helped me, hope they work for you too.

            @shahrukh , that was a great tutorial, keep up the good work!!

  3. Hello sir all are working proper till last but in last it gives error
    after clicking email for action

    it give to

    Hi devendra!

    and when i click to active link it gives error

    Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404

    Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.6.3

    • In your configuration.php,

      change this line into your own URL

      define(‘SITE_URL’, ‘http://your_own_URL’);

      so when you click on “CLICK TO ACTIVATE YOUR ACCOUNT”, it will redirect you to you own “activation” page.

  4. how can i check username and email availability both using this code, i tried but im getting error cannot use try without catch or finally…please help..

  5. will you tell me how to check duplicate email address exit in database or not at the time of registration

  6. Stan Smith Adidas on

    Why viewers still use to read news papers when in this technological
    globe everything is accessible on net?

  7. I Keep getting SMTP ERROR: Failed to connect to server: (0) SMTP connect() failed.
    I’m currently hosted not using local server.

  8. hello!
    my program is working properly but i could not get an email in my email id. which i entered at time of registration.

  9. smtp() connect failed. first error and when I removed $mail->isSMTP(); COULD NOT INSTANTIATE MAIL FUNCTION , Not able to resolve this, can you please help and also update php mailer files.
    right now I am trying with gmail account.

  10. Fatal error: Class ‘SMTP’ not found in D:\Website Project\email\mail\class.phpmailer.php on line 1466

    when i click on submit, this error come up on screen. but information is added to the database
    I dnt get any email. What should i do. please let me know. i want to talk to you on facebook as well.

  11. wedding planner trivandrum on

    Wow that was unusual. I just wrote an really long comment but after I clicked submit my comment didn’t appear.
    Grrrr… well I’m not writing all that over again.
    Anyways, just wanted to say wonderful blog!

  12. Тhank you for every օther informative blog. Ԝɦere
    eⅼse could Ⅰ get that type ⲟf info wгitten іn such a perfect method?
    Ι ɦave а venture thɑt I’m simply now running ⲟn, and
    I Һave Ьeen at the look out fߋr sucҺ infoгmation.

  13. Can you help me?google is blocking my email activation email because of they think that activation link is suspicious…plz help

    this is a function

    email($register_data[’email’],$register_data[‘first_name’],’Active your account’,”
    Hello “. $register_data[‘first_name’] .”\n\nyou need to activate your account, so use the link below: http://localhost/lr/activate.php?email=“. $register_data[’email’] . “&email_code=” . $register_data[’email_code’]);

  14. @Shahrukh Khan
    I have one requirement like, I can add any number of members to my team(or Org) similarly another team also can add a member of my team to their team(or Org) vise versa. While i am adding when i enter mail id of a particular member(if already a member of one Team/Org) and click on submit then mail should go like please use your credentials and login. If the member is not part of any team then mail should go like please use following email id and password to login.
    How can i do this? I am using PHP codeigniter

  15. Md Tausif Jawaid on

    i am not able to create validation form on my own rule please help me .
    i am using database MySql and server communicate language PHP

  16. good eve bro may i ask
    how i can solve this problem i can sent email yet i can’t receive email any answers will be appreciated

  17. devashis khandelwal on

    Awesome post,
    Sir, my status is not changing from “PENDING” TO “APPROVED ” after 11 entries i have already change the length of id to 100.Please help

  18. “An email has been sent for verfication.” but there’s no email came. I tried this in hostinger free account. How would I know if they support emailing? Can I use email forwarding? How?

  19. SQLSTATE[42S22]: Column not found: 1054 Unknown column ’email’ in ‘where clause’ is what i got please help me

  20. Hi Shahrukh, firstly u are awesome, thanks for your tutorials, they are really dandy and outstanding,

    Thumbs up with a standing ovation across borlands online.

    I have an issue, can you please fix the download link to this registration demo, I am failing to download


Leave A Reply

CommentLuv badge

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.