我正在尝试创建一个短信验证系统,仅仅是为了学习。 所以文件结构将遵循
Index.php Form.php Sendcode.php Verify.php
我在这里尝试实现的是尝试学习,如何强制登录用户被重定向到form.php,如果他们验证他们的号码被重定向回index.php,否则提示验证他们的号码在form.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();
}
}
?>
答案 0 :(得分:0)
Ricky from Twilio here.
We put together a tutorial showing how to build this application using Authy. Although this may not be your exact setup looking at how we structured the sample app may help.
For an app more like what you're putting together you could also take a look at this blog post detailing how to build a simple phone verification system with Twilio, PHP, MySQL and jQuery.
Hope these resources help!