要发送到数据库的数据?

时间:2017-01-25 08:55:34

标签: php html database

我正在为我的一个客户使用OTP生成。我已完成以下编码,但我需要将数据发送到数据库,在验证OTP后应存储数据。下面是我尝试过的代码,OTP对我有用。

<?php

    session_start();
    session_set_cookie_params(180);


    $smsgatewaycenter_com_url = "http://URL/api/v3/?"; //SMS Gateway Center API URL
    //$smsgatewaycenter_com_mask = "SGCSMS"; //Your Approved Sender Name / Mask
    $api_key = "apikey";



    function smsgatewaycenter_com_Send($mobile, $sendmessage, $debug=false){
        global $smsgatewaycenter_com_url,$api_key;
        //$parameters = 'UserName='.$smsgatewaycenter_com_user;
       // $parameters.= '&Password='.$smsgatewaycenter_com_password;
        $parameters.= 'method=sms';
        $parameters.= '&api_key='.$api_key;
        $parameters.= '&to='.urlencode($mobile);
        $parameters.= '&sender=VLDEMO';
        $parameters.= '&message='.urlencode($sendmessage);
        $apiurl =  $smsgatewaycenter_com_url.$parameters;
        $ch = curl_init($apiurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $curl_scraped_page = curl_exec($ch);
        curl_close($ch);

        if ($debug) {
            echo "Response: <br><pre>" . $curl_scraped_page . "</pre><br>";
        }
        return($curl_scraped_page);
    }

    /////////////////////////////////////////////////////////////////////////////
    //      Function to generate and append OTP code within the message        //
    /////////////////////////////////////////////////////////////////////////////
    function smsgatewaycenter_com_OTP($length = 8, $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRQSTUVWXYZ0123456789'){
        $chars_length = (strlen($chars) - 1);
        $string = $chars{rand(0, $chars_length)};
        for ($i = 1; $i < $length; $i = strlen($string)){
            $r = $chars{rand(0, $chars_length)};
            if ($r != $string{$i - 1}) $string .=  $r;
        }
        return $string;
    }

    /////////////////////////////////////////////////////////////////////////////
    //           If Debug is set to true below, then response from             //
    //         SMSGatewayCenter.com API will be printed on the screen          //
    /////////////////////////////////////////////////////////////////////////////
    $debug = true; //Set to true if you want to see the response

    /////////////////////////////////////////////////////////////////////////////
    //     If user has not posted anything, lets load the user login page      //
    /////////////////////////////////////////////////////////////////////////////

    if (empty($_POST)){
        $i = 0;
        echo '  
        <html>
<head>
<title>Droom Form</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript">

</script>
<style>
.hide{
  display: none;
}
.show{
  display: block;
}
</style>
</head>
<body>

<header>
<div class="row" style="background-image: url(images/Background_final.png);background-repeat: no-repeat;background-position: cover; ">
<div class="col-md-12">
<div class="col-md-4" >
<div style="padding:10px;text-align: center;background-color:#5e97f2;">
<h4 style="text-size:28px;font-weight:800;color:#fff;">Get Assistance</h4>
</div>
<form name="leadform" method="post" data-toggle="validator" onsubmit="return validateform()" style="padding-left:30px;padding-right:30px;padding-top:20px;padding-bottom: 20px; background-color:#fff;">
<div class="form-group row">
  <label for="example-text-input" class="col-2 col-form-label">Full Name</label>
  <div class="col-4">
    <input class="form-control" type="text" name="firstname" placeholder="Enter your full name" id="example-text-input" required>
  </div>
</div>

<div class="form-group row">
  <label for="example-email-input" class="col-2 col-form-label">Email</label>
  <div class="col-4">
    <input class="form-control" type="text" name="email" placeholder="Enter your Email id" id="example-email-input" required>
  </div>
</div>

<div class="form-group row">
  <label for="example-tel-input" class="col-2 col-form-label">City</label>
  <div class="col-4">
    <input class="form-control" type="text" name="city" placeholder="Enter your city" id="example-tel-input" required>
  </div>
</div>


<div class="form-group row">
  <label for="example-datetime-local-input" class="col-2 col-form-label">Category</label>
  <div class="col-4">
  <select class="form-group" name="category" required>
  <option>---Select--</option>
  <option>Cars</option>
  <option>Bikes</option>
  <option>Other</option>
    </select>
  </div>
</div>
<div class="form-group row">
  <label for="example-search-input" class="col-2 col-form-label">Mobile Number</label>
  <div class="col-4">
    <input class="form-control" type="text" name="mobilenumber" placeholder="Enter your mobile number" id="example-search-input" required>
  </div>
</div>
<input type=submit name="sendsms" value="Get OTP" OnClick="smsgatewaycenter_com_Send(this.form);">

<!--<button type="submit" class="submit hide" id="add">Submit</button>-->

<div>
<!--<input type="submit" id="add" value="Submit">-->
</form>
</div>
</div>
<div class="col-md-8" style="padding-right:0pc;padding-left:0px;">
<img src="images/transparent.png" class="img-responsive" style="height:480px;"/>
</div>
</div>
</div>

</header>
<section>
<div class="container">
<div class="row">
<div class="col-md-12">
<h4 style="font-family:Verdana;font-size:32px;padding-top:40px;text-align: center;">Because you dont buy a car everyday!</h4>
<div class="col-md-12">
<div class="col-md-3">
<img class="img-responsive" src="images/widest_selection.png"/>
<p style="font-size:16px;"><b>Widest Selection</b></p>
</div>
<div class="col-md-3">
<img class="img-responsive" src="images/lowest_price.png" width="150" height="150"/>
<p style="font-size:16px;"><b>Lowest Price Guranteed</b></p>
</div>
<div class="col-md-3">
<img class="img-responsive" src="images/verified_seller.png"/>
<p style="font-size:16px;"><b>Verified Seller</b></p>
</div>
<div class="col-md-3">
<img class="img-responsive" src="images/buyer_protection.png"/>
<p style="font-size:16px;"><b>100% Buyer Protection</b></p>
</div>

</div>
</div>

</div>
</section>
<footer>
<p style="font-size:15px;text-align:center;">Copyright &copy; 2017 | All rights reserved</p>
</footer>





</body>
</html>';
    }


    if (isset($_POST['sendsms'])){
        $_SESSION['smsgatewaycenterotp'] = smsgatewaycenter_com_OTP(); //Generate OTP

        $username = $_POST['firstname'];
        $mobile = $_POST['mobilenumber'];

            smsgatewaycenter_com_Send($mobile,'Dear '.$firstname.'! Please authenticate your OTP. Your One Time password is: '.$_SESSION['smsgatewaycenterotp'],$debug);
            echo '
            <html>
                <body>
                    <h1>Authenticate OTP (One Time Password)</h1>
                    <p>We have sent an SMS to your registered phone number, please authenticate your one time password entering below.</p>
                    <form method="POST">
                    <table border=1>
                        <tr>
                            <td>Your one time password (OTP):</td>
                            <td><input type="text" name="getsmsgatewaycenterotp"></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type=submit name="submitotp" value="Authenticate OTP"></td>
                        </tr>
                    </table>
                    </form>
                </body>
            </html>';;

    }
    elseif (isset($_POST['submitotp'])){

        $sgc_otp = $_POST['getsmsgatewaycenterotp'];
        if($_SESSION['smsgatewaycenterotp'] == $sgc_otp){
            echo '
            <html>
                <body>
                    <h2>You\'ve been successfully verified your One-Time Password</h2>
                </body>
            </html>';
        } else {
            echo'
            <html>
                <body>
                    <h2>Wrong Password!</h2>
                </body>
            </html>';
        }
    }

此代码工作后,数据应发送到数据库。请任何人帮我如何做到这一点。这是我在PHP中的第一个项目。需要帮助。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$message = 'your message with OTP';
$mobile = 'mobile number';
$url = 'URL of your SMS API with urlencode() message in it';

try {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $APIresponse = curl_exec($ch);
    curl_close($ch);
    $res = json_decode($APIresponse, true);     // API response
}
catch(Exception $e)
{
    $response['ErrorCode']  = 2;
    $response['ErrorMsg']   = 'Error in SMS API';
}

if($res['ErrorCode'] == '000')                  // API Response is OK means OTP send on given mobile number
{
    // make a sql query to insert mobile number, otp, text message in database
}