如何比较没有短信发送的重新加载页面的otp号码?

时间:2019-05-29 12:19:31

标签: laravel

我想比较otp号码,我在文本框中键入该密码,然后通过laravel中的api调用控制器将sms otp发送到该号码。

我使用laravel5.6和php 7.2.3

 public function otpverify(Request $req)
    {
        $otpenter=$req->txtotp;
        if ($otpenter==$otp) 
        {
            return redirect()->action('JaincountController@create')
        }
        else
        {
            return view('jaincount_user/verification');
        }
    }
    public function makeRequest(Request $req)
    {
            $client = new Client();
            $otp=rand(10000,4);
            // $data=
            $data = array('adhar'=>$req->txtadharnumber,'drp'=>$req->drpcode,'mobilenumber'=>$req->txtnumber);


            $response = $client->request('POST','http://192.168.1.9/jaincountapi/public/api/otpsms',[
            'form_params'=>[
                'adharcardnumber'=>$req->txtadharnumber,
                'mobilecode'=>$req->drpcode,
                'mobilenumber'=>$req->txtnumber,
                'otp'=>$otp
            ]
            ]);
            $response = $response->getBody();
            return json_decode($response,true);
    }

我想比较通过api调用发送的文本框otp编号和短信otp编号,并使用laravel5.6中的另一个控制器进行重定向

1 个答案:

答案 0 :(得分:0)

问题是您必须将otp存储在数据库或会话变量中。

(文档:https://laravel.com/docs/5.8/eloquent) 您可以将otp存储在数据库中,例如

public function makeRequest(Request $req)
{
        $client = new Client();
        $otp=rand(10000,4);
        // $data=
        $data = array('adhar'=>$req->txtadharnumber,'drp'=>$req->drpcode,'mobilenumber'=>$req->txtnumber);

        //CHANGES
        User::where('phone_number',$req->txtnumber)->update(['otp'=>$otp]);


        $response = $client->request('POST','http://192.168.1.9/jaincountapi/public/api/otpsms',[
        'form_params'=>[
            'adharcardnumber'=>$req->txtadharnumber,
            'mobilecode'=>$req->drpcode,
            'mobilenumber'=>$req->txtnumber,
            'otp'=>$otp
        ]
        ]);
        $response = $response->getBody();
        return json_decode($response,true);
}

您可以使用

在Laravel中雄辩地检索它
public function otpverify(Request $req)
 {
    $otpenter=$req->txtotp;
    //CHANGES
    $otp = User::where('phone_number', $phone_number)->first()->otp;

    if ($otpenter==$otp) 
    {
        return redirect()->action('JaincountController@create')
    }
    else
    {
        return view('jaincount_user/verification');
    }
}

输入正确的otp后,请清除数据库中的内容。

或者您可以使用会话。您可以通过两种方式使用会话

1.php默认会话 2.Laravel会话

让我们看看php默认会话 (文档:https://www.php.net/manual/en/book.session.php

public function makeRequest(Request $req)
   {
        $client = new Client();
        $otp=rand(10000,4);
        // $data=
        $data = array('adhar'=>$req->txtadharnumber,'drp'=>$req->drpcode,'mobilenumber'=>$req->txtnumber);

        //CHANGES
        session_start();
        $_SESSION['otp'] = $otp


        $response = $client->request('POST','http://192.168.1.9/jaincountapi/public/api/otpsms',[
        'form_params'=>[
            'adharcardnumber'=>$req->txtadharnumber,
            'mobilecode'=>$req->drpcode,
            'mobilenumber'=>$req->txtnumber,
            'otp'=>$otp
        ]
        ]);
        $response = $response->getBody();
        return json_decode($response,true);
}

您可以通过以下方式检索它

public function otpverify(Request $req)
 {
    $otpenter=$req->txtotp;

    //CHANGES
    session_start();
    $otp = $_SESSION['otp']


    if ($otpenter==$otp) 
    {
        return redirect()->action('JaincountController@create')
    }
    else
    {
        return view('jaincount_user/verification');
    }
}

让我们使用laravel会话 (文档:https://laravel.com/docs/5.2/session

//important 
use Illuminate\Support\Facades\Session;


public function makeRequest(Request $req)
   {
        $client = new Client();
        $otp=rand(10000,4);
        // $data=
        $data = array('adhar'=>$req->txtadharnumber,'drp'=>$req->drpcode,'mobilenumber'=>$req->txtnumber);

        //CHANGES
        Session::put('otp',$otp)


        $response = $client->request('POST','http://192.168.1.9/jaincountapi/public/api/otpsms',[
        'form_params'=>[
            'adharcardnumber'=>$req->txtadharnumber,
            'mobilecode'=>$req->drpcode,
            'mobilenumber'=>$req->txtnumber,
            'otp'=>$otp
        ]
        ]);
        $response = $response->getBody();
        return json_decode($response,true);
}

您可以通过以下方式检索它

//important 
use Illuminate\Support\Facades\Session;

public function otpverify(Request $req)
 {
    $otpenter=$req->txtotp;

    //CHANGES
    $otp = Session::get('otp') //best way to use is flash. see the full documentation


    if ($otpenter==$otp) 
    {
        return redirect()->action('JaincountController@create')
    }
    else
    {
        return view('jaincount_user/verification');
    }
}