【问题标题】:Secure a contact form from scratch in Wordpress在 Wordpress 中从头开始保护联系表单
【发布时间】:2020-11-18 07:24:21
【问题描述】:

我目前正在使用 Wordpress 创建一个网站,我正在创建我的主题并且我没有使用 jQuery。我需要介绍一个简单的联系表单,它会在提交时发送一封电子邮件,并且所有插件都需要 jquery 才能工作。

创建发送电子邮件的联系表单是否安全?提交时不查询数据库,是否存在 SQL 注入风险?

我的安全技能很少,欢迎任何信息或澄清

【问题讨论】:

    标签: php sql database wordpress email


    【解决方案1】:

    例如:

    $name = "{$_POST['message_name']} {$_POST['message_lastname']}"; // I like to combine first and lastname in to 1 variable.
    
    $email = $_POST['message_email'];
    $website = $_POST['message_url'];
    $message = $_POST['message_description'];
    
    if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { 
     $response = form_validation_response( 'error', $email_invalid );
    } else {
     if ( empty( $name ) || empty( $message) ) {
      $response = form_validation_response( 'error', $missing_content );
     }
    }
    
    // The most simple check you can do is make sre that the fields are NOT empty. 
    

    form_validation_response 方法是一个简单的函数,可以用来返回错误信息:

    $not_human       = "Human verification incorrect.";
    $missing_content = "Please supply all information.";
    $email_invalid   = "Email Address Invalid.";
    $message_unsent  = "Message was not sent. Try Again.";
    $message_sent    = "Thanks! Your message has been sent.";
    
    function form_validation_response( $type, $message ) {
     $class = 'px-2 py-1 mb-6 rounded-md' // These are tailwind classes, but it could be bootstrap
     if ( $type == 'success' ) {
        $class .= "border border-green-800 text-green-700";
     } else {
        $class .= "border border-redish text-redish";
     }
    
     return "<div class='{$class}'>{$message}</div>";
    }
    

    上面的示例用于验证电子邮件,但您也可以在开始验证过程之前确保字段已实际提交:

    【讨论】:

    • 感谢您提供这些基础,非常有用!如果我理解正确,是 filter_var () 函数保护并验证用户只提交预期值。但是如果没有这个功能,会有什么风险呢?用户在服务器端运行 PHP 代码?
    【解决方案2】:

    如果您不熟悉创建“安全”的 php 表单,我建议您为此使用插件。

    如果您“允许”安装插件,请查看“表单插件”,例如:

    这只是一些可用的表单插件。然后,根据您的需要/预算,您应该决定哪个插件最适合(有些是免费、免费增值、高级等)。

    【讨论】:

    • 感谢您的回答:)。我没有使用插件的授权,我可以完全投入到表单的创建中,如果它不够安全,会有什么风险? (如果你知道任何文件,我会很高兴)
    • 嗯,在这种情况下,我建议在提交表单时构建一个表单,以确保验证输入字段($_POST 数据)。我将在下一条评论中展示一个简单的示例,1 秒。
    猜你喜欢
    • 1970-01-01
    • 2012-08-18
    • 1970-01-01
    • 2021-09-14
    • 2012-04-20
    • 2021-01-26
    • 2017-12-18
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多