繁体   English   中英

使用twilio创建短信验证系统

[英]Creating 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();
    }
}
?>  

验证.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。

我们汇总了一个示例应用程序,该示例程序显示了SMS帐户验证,在这里可能会有帮助:

https://www.twilio.com/docs/tutorials/walkthrough/account-verification/php/laravel

我们使用了Authy,这是专门用于这种用例的Twilio产品构建。 分解几个部分以与您当前的代码进行比较。 这是我们发送短信的部分:

$authyUser = $authyApi->registerUser($newUser->email, $newUser->phone_number, $newUser->country_code);
if($authyUser->ok())
{
    $newUser->authy_id = $authyUser->id();
    $newUser->save();
    $request->session()->flash(
        'status',
        "User created successfully"
    );

    $sms = $authyApi->requestSms($newUser->authy_id);
    DB::commit();
    return redirect()->route('user-show-verify');
}

这是我们验证用户输入的代码的位置:

public function verify(Request $request, Authenticatable $user, AuthyApi $authyApi, TwilioRestClient $client)
{
    $token = $request->input('token');
    $verification = $authyApi->verifyToken($user->authy_id, $token);

    if ($verification->ok())
    {
        $user->verified = true;
        $user->save();
        $this->sendSmsNotification($client, $user);

        return redirect()->route('user-index');
    }
    else
    {
        $errors = $this->getAuthyErrors($verification->errors());
        return view('verifyUser',['errors' => new MessageBag($errors)]);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM