Simple Phone Verification with Twilio, PHP, MySQL and jQuery

PHP
jQuery
MySQL

See the most updated version of these instructions and sample code here or read the tutorial on how to build phone verification in a Rails app

As more and more applications integrate with phones there is an increasing need to verify phone numbers to help prevent spam, fraud and other dubious activities. In these cases it’s common to use an automated phone verification system to present the user a verification code in their web browser while a call is initiated to their phone. When the call is answered they are prompted to enter the code, allowing them to confirm that they are the owner of that number.

In this post I’m going to cover how to build a simple phone number verification system using PHP, MySQL and jQuery.

This is a long example so I’m only going to highlight the most important parts in this post. You can download the complete example here. To run this example on your own server, update the database settings in database.php and your Twilio account settings in call.php. This example also makes use of our Twilio PHP Helper library which you can find on GitHub.

Basic steps

  1. User visits verification web page and enters phone number.
  2. Random verification code is generated and user is called and prompted to enter code.
  3. If code is entered incorrectly, re-prompt to enter code.
  4. If code is entered correctly, update database.
  5. Update web page with status message.

Step One: The verification web page

We’ll start with the HTML the user will see. Create a file called index.php and add the following code to the <body>:

Enter your phone number:

When the user visits the page they’ll be presented with the form asking them for their phone number. Once entered, we’ll hide the form and show them their verification code. Here’s the jQuery needed to make that transition:

Our initiateCall() function kicks off an AJAX request to generate the random code and initiate the phone call, which I’ll discuss in more detail in Step Two later. Once we’ve got the code, we’ll update the UI to show it to the user and then start polling our database for status updates. The code to poll the server looks like this:

Once we’ve got confirmation from the server that the number has been verified, we update the UI to let the user know.

Step Two: Generate and display random code

Step Two in our walkthrough overlaps with Step One since it is initiated when the user submits the form with their phone number. We’ll pick up in our initiateCall() function which makes the AJAX POST request to call.php. Inside call.php we generate the random 6-digit verification code and start the phone call by calling the Twilio REST API using the PHP Helper Library. Once we have the code, we’ll send it back as JSON for the browser to parse and display (as covered in Step One).

We’re storing the phone number along with the generated code in a MySQL database so that we can update the verified column from our TwiML script and then retrieve it later. For this example I’m using a simple table definition:

Steps Three and Four: Collect and verify code via phone call

When initiating the call we direct Twilio to use the twiml.php file which generates the necessary TwiML to prompt the caller to enter their code. The first time this file is requested we’ll ask the caller to enter their code. Once they’ve entered 6 digits, Twilio will make another post to the same URL with the Digits POST parameter now included. With this information we look up the caller’s phone number in the database and check for a match. If the code entered is incorrect, we’ll re-prompt them to enter it again. Once they’ve entered the correct code we update the database and thank them for calling.

Step Five: Update the web page when the number is verified

Back in Step One we created the jQuery needed to begin polling the server for status updates. Our checkStatus() function makes a POST to status.php which looks up the phone number in the database and sends back some JSON containing the verification status.

Coming soon: Verification via SMS

In an upcoming post I’ll be using this example as a starting point for creating a similar system to verify phones but using SMS instead of voice. Let me know in the comments below if there’s anything you’d like to see covered in that post.

Download the complete example

Download twilio-phone-verification.zip

  • Syed Zeeshan Ali Naqvi

    great great work John. Thanks man. I was really looking for it and please do let me know when the SMS authentication is gonna come up. I shall be very thankful. Its just awesome. A++++++++.

  • Syed Zeeshan Ali Naqvi

    great great work John. Thanks man. I was really looking for it and please do let me know when the SMS authentication is gonna come up. I shall be very thankful. Its just awesome. A++++++++.

  • Pingback: Quora()

  • Pingback: Quora()

  • Deepak

    nice example

  • Deepak

    nice example

  • Binny

    Hello Sir,

    I have used your code, but getting problem in it. My client gave me AccountSid and AuthToken and I have applied same functinality according to you. but I am facing problem in it. I have call correctly but I after enter verification code , call automatically disconnect and I have checked in database one entry in numbers table but verified status not updated.

    Please let me know why this is happen.

    Thanks
    Binny

    • http://www.twilio.com Twilio

      We have an updated post and code samples here: http://www.twilio.com/blog/2012/04/build-a-simple-phone-verification-with-twilio-php-mysql-and-jquery.html 

      Take a look as it may help with what you are building. If not, email help@twilio.com and we can walk you through it.

  • Binny

    Hello Sir,

    I have used your code, but getting problem in it. My client gave me AccountSid and AuthToken and I have applied same functinality according to you. but I am facing problem in it. I have call correctly but I after enter verification code , call automatically disconnect and I have checked in database one entry in numbers table but verified status not updated.

    Please let me know why this is happen.

    Thanks
    Binny

    • http://www.twilio.com Twilio

      We have an updated post and code samples here: http://www.twilio.com/blog/2012/04/build-a-simple-phone-verification-with-twilio-php-mysql-and-jquery.html 

      Take a look as it may help with what you are building. If not, email help@twilio.com and we can walk you through it.

  • Frax

    What’s the best way to do this in python?
    I’m writing a python server and need verification via pin entry.

    Thanks

  • http://www.facebook.com/sivaaccet Sivakumar Ganesan

    How to extend the call duration?, because while entering verification code, the call aromatically ends. please help.

  • mark thomas

    This is perfect for us, we would send thousands of SMS, can I ask if this can be added in to a IPN system, eg: our own store and button – paypal, IPN our own database – our database checks the email address, if new then send SMS, if not it means customer has ordered before and we don’t need to verify him/her, can this be done? or do we have to use everything including button from you?
    Thanks

    • http://twitter.com/CaseySoftware D. Keith Casey, Jr.

      Please feel free to take this code and customize it, add things, remove things however you wish.

      As is, this code most likely won’t fit your needs but the goal you describe is entirely possible.

    • sam

      mark, we are looking for a very similar solution to what you’re looking for. Maybe we could split development costs on this. Give me a call or text 3zero3-5seven9-1544

      Sam

  • Rohan

    Give a try to Dial2Verify’s Missed Call Verification platform. Its OPEN, FREE & Supports multiple countries. http://dial2verify.com

    For knowledgebase on API, visit, http://kb.dial2verify.in

  • Rahul

    Awsom article, has anyone used http://dial2verify.com platform for customer verification implementation.

    OR

    @Twilio,
    I have a twilio account, Can you please share code for functionality as mentioned in
    http://sample.code.dial2verify.in/ I want to implement same with twilio account.

    • http://www.twilio.com Twilio

      Hey Rahul, I’d love to learn more about what your building and include the code you need. Can you email kyleky@twilio.com?

  • nand

    I am developing app in Spring MVC and View part is consist Html,thymleleaf and Java Objects. How can i implement same ?

  • http://www.roydondsouza.com/ Roydon D’ Souza

    Hi @twilio:disqus

    I want my users to call a predefined phone number and my php service will know which user called based on the Mobile Number; based on this I will consider that the user has verified a pending action. More like a missed call verification.

    Can you please point me to the proper service. Thanks.