[英]SMS verification system using Twilio
我正試圖創建一個短信驗證系統,只是為了學習。 因此文件結構如下
Index.php Form.php Sendcode.php Verify.php
我要在這里實現的目的是嘗試學習,如何強制已登錄的用戶重定向到form.php,以及如果他們驗證了他們的號碼后又被重定向回index.php,否則將提示您在表單上驗證他們的號碼。 PHP的錯誤。 因此,如果DB中的狀態設置為1,則用戶可以訪問index.php,否則狀態保持設置為0,並且需要在form.php上驗證數字
有人可以幫我弄這個嗎? 也許寫一個示例index.php代碼
這是文件內容
form.php的
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#phone").submit(function()
{
var phone_no = $('#phone_no').val();
if(phone_no != '')
{
$.post("sendcode.php", { phone_no: phone_no },
function(data)
{
$(".result").html(data);
},
"html"
);
}
return false;
});
});
</script>
<div class = "result"></div>
<p>Enter your phone number below, and we will send you a verification code to that phone number.</p>
<form id = "phone" method = "POST" action = "">
<label for = "phone">Phone number</label>
<input name = "phone" type = "text" id = "phone_no" />
<input name = "submit" type = "submit" value = "Send Verification Code" />
</form>
<p>Enter Verification Code received to the phone number specified above in the form below.</p>
<form id = "verification" method = "POST" action = "verify.php">
<label for = "code">Verification Code</label>
<input name = "code" type = "text" id = "code" />
<input name = "submit" type = "submit" value = "Verify" />
</form>
Sendcode.php
<?php
// configuration
/*** mysql hostname ***/
$hostname = 'localhost';
// database name
$dbname = '';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
// enter SID here
$twilioSid = '';
// enter twilio token here
$twilioToken = '';
if(isset($_POST['phone_no']))
{
try
{
$verifyCode = rand(1000, 9999);
$phone = $_POST['phone_no'];
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
/*** add verification code and phone number to db **/
$sth = "INSERT INTO user (phone, code) VALUES(:phone, :code)";
$command = $dbh->prepare($sth);
$command->bindParam(':phone', $phone, PDO::PARAM_STR);
$command->bindParam(':code', $verifyCode, PDO::PARAM_INT);
$command->execute();
// twilio library
require ('Services/Twilio.php');
$client = new Services_Twilio($twilioSid, $twilioToken);
// send sms with verifcation code
$response = $client->account->sms_messages->create('555-555-555', $phone, 'Verification code ' . $verifyCode);
echo '<p>A verification code was sent to your phone number. Please enter it below.</p>';
/*** close the database connection ***/
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
Verify.php
<?php
if(isset($_POST['code']))
{
$verifyCode = $_POST['code'];
/*** mysql hostname ***/
$hostname = 'localhost';
/*** database name ***/
$dbname = '';
/*** mysql username ***/
$username = 'username';
/*** mysql password ***/
$password = 'password';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
// USER_ID is the login ID of the user
$sql = "SELECT code FROM user WHERE id = {$user_id}";
$sth = $dbh->query($sql);
$code = $sth->fetchColumn();
if($code == $verifyCode)
{
echo "Your account has been validated.";
// verify user in db
$todo = "UPDATE user SET status = 1 WHERE user_id = {$user_id}";
$dbh->execute($todo);
}
else
{
echo "Your account has not been validated.";
}
$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
?>
來自Twilio的Ricky。
我們整理了一個教程,展示了如何使用Authy構建此應用程序。 盡管這可能不是您的確切設置,但看看我們如何構造示例應用程序可能會有所幫助。
對於更像您要組合在一起的應用程序,您還可以查看此博客文章,詳細介紹如何使用Twilio,PHP,MySQL和jQuery構建簡單的電話驗證系統 。
希望這些資源有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.