## Step 4: Controller Integration

Ok, it's time to go back to our signup controller. Go to the function process() and enter the following code after the database insertion:

First, we check the country code, if it's 1 ( i.e. United States ) we will use SmsMatrix, so we load it with \$this->load->library('smsmatrix_api'). If the country is not United States, we load the clickatell_api. Either way, we use send_sms() to send the activation code to the mobile number. You'll notice the mobile number is inserted before in the database by concatenating the country value ( which holds the prefix for that country ) with the mobile number. We could actually complete the second parameter in the library load to access it with a certain name, and only use send on the library:

This way, we load only the appropriate library with the same name, accesing it with 'sms_api', but it's the same thing. Basically the two libraries share the same interface. Further, if you get false as a return value, you can send the SMS with the other library as a backup.

If we set a 'signed' cookie after sending the SMS message, we can use it later to prevent multiple messages from beign sent. We need to do it in such a way that when the user navigates away from the signup form and later goes back to try and signup again, we will automatically redirect him to the activate function. Add this in the top of the signup() function:

This way, if you signup once and you try to access the signup form again, you will get to the same activate function that tells you to enter the activation code. Let's take care of the activation next.

## Step 6: The Activation Function

We'll now create the activation process:

Let me explain what is going on here: I do the reverse, if the cookie is not set I redirect to signup. Then, I set the error message to '', because we don't have any error now. If the signup post field has been set (i.e. if the form has been submitted), I look for an entry in the table with the id from the cookie and the activation equal to the code submitted. It's important to never have an id of 0 in the database, as this would make the cookie false, and there would be a redirect loop. This check can be done, but it's better to set it in the database.

I count the rows returned, and if I don't get any, I show an error that the authorization is not correct. As the user can only have a certain activation code, when you enter the wrong code, the database won't find any row and return 0. If we find a row, we delete the cookie, as we don't need it anymore, we set the active column to 1 and the activation to '' and update the table. From this point on, the user is activated and we know for sure that it's his phone, and we can probably send a special offer as an sms. I also redirect the user to the success() function, which is a simple view with this success message:

## Step 7: The Final Controller Code

This is the final signup controller code:

## Conclusion

Thanks for reading this tutorial! As the outcome of this tutorial is small, and testing is expensive, we don't have a preview, but you can run this from the sources provided with your own test accounts. This is only a small part of what can be done with Clickatell and SmsMatrix. There are a lot of things you can try, like sending to multiple phones, mass advertising, e-mailing a voice file to create a voice message, TTS messages and so on. I hope you enjoyed my tutorial, and I hope you use it in your next generation SMS application! Thank you for following my tutorial and write in the comments what you have come up with!