联系表格安全

时间:2019-02-08 12:38:28

标签: php forms security

我有一个很长的联系表格要处理(业主要求)。我有reCAPTCHA,但是访问者输入个人信息(姓名,电子邮件和电话号码)时不需要安全吗?有没有简单的方法可以为现有表单增加安全性?该表格确实可以验证并且可以正常运行。有人愿意帮助我吗?我不懂PHP。感谢您的帮助。这是相关代码:

<?php
require( 'recaptcha-master/src/autoload.php' );

$siteKey = 'Key.....';
$secret = 'Secret....';

$recaptcha = new\ ReCaptcha\ ReCaptcha( $secret, new\ ReCaptcha\ RequestMethod\ SocketPost() );

if ( !empty( $_POST )and isset( $_POST[ 'g-recaptcha-response' ] ) ):

    $gRecaptchaResponse = $_POST[ 'g-recaptcha-response' ]; //google captcha post data
$remoteIp = $_SERVER[ 'REMOTE_ADDR' ]; //to get user's ip

$recaptchaErrors = ''; // blank varible to store error

$resp = $recaptcha->verify( $gRecaptchaResponse, $remoteIp ); //method to verify captcha
if ( $resp->isSuccess() ) {
    // send mail or insert in db or do whatver you wish to
    $headers = 'From: CBS.Website';
    $emailbody = 'Name: ' . $_POST[ 'name' ] . "\n"
    . 'Company: ' . $_POST[ 'company' ] . "\n"
    . 'Email: ' . $_POST[ 'email' ] . "\n"
    . 'Phone: ' . $_POST[ 'phone' ] . "\n"
    . 'Referral: ' . $_POST[ 'referral' ] . "\n"
    . 'Stage: ' . $_POST[ 'stage' ] . "\n"
    . 'Budget Range: ' . $_POST[ 'budget' ] . "\n"
    . 'Geographic Location: ' . $_POST[ 'location' ] . "\n"
    . 'Type: ' . $_POST[ 'type' ] . "\n"
    . 'Special Considerations: ' . $_POST[ 'considerations' ] . "\n"
    . 'Project Type: ' . $_POST[ 'projectType' ] . "\n"
    . 'Square Footage: ' . $_POST[ 'sqfootage' ] . "\n"
    . 'Number of Stories: ' . $_POST[ 'stories' ] . "\n"
    . 'Schedule: ' . $_POST[ 'schedule' ] . "\n"
    . 'SF Budget: ' . $_POST[ 'sfbudget' ] . "\n"
    . 'Future Construction: ' . $_POST[ 'futureConst' ] . "\n"
    . 'Special Features: ' . $_POST[ 'features' ] . "\n"
    . 'Restrictions: ' . $_POST[ 'restrictions' ] . "\n";

    mail( 'myemail@mail.com', 'Contact Form Submission', $emailbody, $headers );
            echo "<meta http-equiv='refresh' content=\"0; url=thankyou.php\">";

} else {
    $recaptchaErrors = $resp->getErrorCodes(); // set the error in varible
}
endif;
?>


 <script>
             function MM_validateForm() { //v4.0
            if ( document.getElementById ) {
                var i, p, q, nm, test, num, min, max, errors = '',
                    args = MM_validateForm.arguments;
                for ( i = 0; i < ( args.length - 2 ); i += 3 ) {
                    test = args[ i + 2 ];
                    val = document.getElementById( args[ i ] );
                    if ( val ) {
                        nm = val.name;
                        if ( ( val = val.value ) != "" ) {
                            if ( test.indexOf( 'isEmail' ) != -1 ) {
                                p = val.indexOf( '@' );
                                if ( p < 1 || p == ( val.length - 1 ) ) errors += '- ' + nm + ' must contain an e-mail address.\n';
                            } else if ( test != 'R' ) {
                                num = parseFloat( val );
                                if ( isNaN( val ) ) errors += '- ' + nm + ' must contain a number.\n';
                                if ( test.indexOf( 'inRange' ) != -1 ) {
                                    p = test.indexOf( ':' );
                                    min = test.substring( 8, p );
                                    max = test.substring( p + 1 );
                                    if ( num < min || max < num ) errors += '- ' + nm + ' must contain a number between ' + min + ' and ' + max + '.\n';
                                }
                            }
                        } else if ( test.charAt( 0 ) == 'R' ) errors += '- ' + nm + ' is required.\n';
                    }
                }
                if ( errors ) alert( 'The following error(s) occurred:\n' + errors );
                document.MM_returnValue = ( errors == '' );
            }
        }
    </script>
    <script src='https://www.google.com/recaptcha/api.js'></script>

表格:

 <form class="contactForm" action="contact.php" method="POST" title="Contact CBS Structures, Inc.">

                     <p>
                    <label>* Name</label> <span class="tiny-text">(first/last)</span>:
                    <input name="name" type="text" required="required" class="inputFieldsR" id="name" tabindex="1" size="50" maxlength="40"/>
                </p>
                    <p>
                    <label>* Company</label>
                    <input name="company" type="text" required="required" class="inputFieldsR" id="company" tabindex="2" value="" size="50" maxlength="40"/>
                </p>
                    <p>
                    <label>* E-mail</label>
                    <input name="email" type="text" required="required" class="inputFieldsR" id="email" tabindex="3" value="" size="50" maxlength="25"/>
                </p>

                <p>
                    <label>* Phone</label><span class="tiny-text"> (numbers only)</span>
                    <input name="phone" type="text" required="required" class="inputFieldsR" id="phone" tabindex="4" value="" size="50" maxlength="50"/>
                </p>
                <label>* Please tell us who referred you to CBS Structures, Inc.</label>
                <input name="referral" type="text" required="required" class="inputFieldsR" id="referral" tabindex="16" value="" size="50" maxlength="50"/>
                </p>
                <p>
                    <label>Stage</label><span class="tiny-text"> (Planning/Proposal/Funded)</span>
                    <input name="stage" type="text" class="inputFields" id="stage" tabindex="5" value="" size="50" maxlength="50"/>
                </p>

                <p>
                    <label>Budget Range</label>
                    <input name="budget" type="text" class="inputFields" id="budget" tabindex="6" value="" size="50" maxlength="50"/>
                </p>

                <p>
                    <label>Geographic Location</label>
                    <input name="location" type="text" class="inputFields" id="location" tabindex="7" value="" size="50" maxlength="50"/>
                </p>

                <p> <label>Type (Select one)</label>
                    <select name="type" size="1" id="type" tabindex="8">
                         <option value="Permanent" selected="selected">Permanent</option>
                        <option value="Semi-permanent">Semi-Permanent</option>
                    </select>
                </p>
            <br><br>
                    <p>
                    <label>Special Considerations</label><br>
                    <textarea name="considerations" cols="50" class="textareaFields" rows="9" maxlength="350" id="considerations" tabindex="10"></textarea>
                </p><br>

                <p class="radio">Project Type:
                </p>
                <div class="radioBtns">
                    <div class="radioColumn1">
                        <label>
           <input type="radio" name="projectType" value="Commercial" id="projectType_0" tabindex="10">
             Commercial</label>

                        <br>
                        <label>
             <input type="radio" name="projectType" value="Institutional" id="projectType_1">
             Institutional</label>

                        <br>
                        <label>
             <input type="radio" name="projectType" value="Municipal" id="projectType_2">
             Municipal</label>

                    </div>
                    <div class="radioColumn2">
                        <label>
             <input type="radio" name="projectType" value="Military Use" id="projectType_3">
             Military Use</label>

                        <br>

                        <label>
             <input type="radio" name="projectType" value="Residential" id="projectType_4">
             Residential</label>

                        <br>
                        <label>
             <input type="radio" name="projectType" value="Other" id="projectType_5">
             Other</label>

                    </div>
                </div>
                <br>
                <p>
                    <label>What are the square footage requirements?</label>
                    <input name="sqFootage" type="text" class="inputFields" id="sqFootage" tabindex="11" value="" size="50" maxlength="50"/>
                </p>
                <p>
                    <label>Is the building(s) one story or multi-story?</label>
                        <input name="stories" type="text" class="inputFields" id="stories" tabindex="12" value="" size="50" maxlength="50"/>
                </p>

                <p>
                    <label>What is your desired building schedule?</label>
                    <input name="schedule" type="text" class="inputFields" id="schedule" tabindex="13" value="" size="50" maxlength="50"/>
                </p>

                <p>
                    <label>What is your SF budget?</label>
                    <input name="sfbudget" type="text" class="inputFields" id="sfbudget" tabindex="14" value="" size="50" maxlength="50"/>
                </p>

                <p>
                    <label>Do you anticipate future construction or building phases on this site or region?</label>
                    <input name="future" type="text" class="inputFields" id="future" tabindex="15" value="" size="50" maxlength="50"/>
                </p>

                <p>
                    <label>Does the site have any special features or views that need to be taken into consideration?</label><br>
                    <textarea name="features" cols="60" class="textareaFields" rows="6" maxlength="300" id="features" tabindex="16"></textarea>
                </p>
                <br>
                <p>
                    <label>Are there any specific site restrictions?</label><br>
                    <textarea name="restrictions" cols="60" class="textareaFields" rows="6" maxlength="300" id="restrictions" tabindex="17"></textarea>
                </p><br>


                <br>
                <div class="g-recaptcha" data-sitekey="6LcM_wkUAAAAALpmzA-yLgvqo1xLoBRnfuZXWMf_"></div>
                <br>
                <p>
                    <input style="color:#fff; font-size: 1.2em; width:120px; height: 40px; background-color: #246473" name="submit" type="submit" formmethod="POST" onClick="MM_validateForm('name','','R','company','','R','email','','RisEmail','phone','','RisNum','R','referral','R');return document.MM_returnValue" value="Submit">
                </p>   
            </form>

0 个答案:

没有答案