dompdf 表单在安全服务器 (ssl) 中无法正常工作

dompdf form not working properly in secure server (ssl)

我使用 dompdf 创建了一个表单,当我将其上传到网站所属的常规服务器时,它运行良好。但是,当我将它移至安全服务器 (ssl) 时,它无法正常工作,也没有发送表单。服务器运行 Linux。 Web 服务器是带有 FastCGI 的 Nginx。关于可能发生的事情的任何想法?我刚从表格中收到一条错误消息,说并非所有必填字段都已填写,但只有两个,我填写了。

2015/09/10 22:07:35 [error] 14522#0: *1509 FastCGI sent in stderr: "PHP 
message: PHP Warning: strip_tags() expects parameter 1 to be string, 
array given in 
/var/www/nuweights.net/html/patient_registration_form/form.php on line 13
PHP message: PHP Warning: strip_tags() expects parameter 1 to be 
string, array given in 
/var/www/nuweights.net/html/patient_registration_form/form.php on line 13
PHP message: PHP Notice: Undefined property: stdClass::$prim_phone in 
/var/www/nuweights.net/html/patient_registration_form/pdf.php on line 
180" while reading response header from upstream, client: 72.83.230.123, 
server: nuweights.net, request: "POST 
/patient_registration_form/form.php HTTP/1.1", upstream: 
"fastcgi://unix:/var/run/php5-fpm.sock:", host: "ssl.nuweights.net", 
referrer: "https://ssl.nuweights.net/patient_registration_form/form.php"

这是 php 文件的代码。如果我需要更多验证,我该如何添加它?代码是什么?

<?php
if (!empty($_POST)) {

// Used for later to determine result
$success = $error = false;

// Object syntax looks better and is easier to use than arrays to me
$post = new stdClass;

// Usually there would be much more validation and filtering, but this
// will work for now.
foreach ($_POST as $key => $val)
$post->$key = trim(strip_tags($_POST[$key]));

// Check for blank fields
if (empty($post->first_name) OR empty($post->last_name))
    $error = true;
else {

    // Get this directory, to include other files from
    $dir = dirname(__FILE__);

    // Get the contents of the pdf into a variable for later
    ob_start();
    require_once($dir.'/pdf.php');
    $pdf_html = ob_get_contents();
    ob_end_clean();

    // Load the dompdf files
    require_once($dir.'/dompdf/dompdf_config.inc.php');
    $dompdf = new DOMPDF(); // Create new instance of dompdf
    $dompdf->load_html($pdf_html); // Load the html
    $dompdf->render(); // Parse the html, convert to PDF
    $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later

    // Get the contents of the HTML email into a variable for later
    ob_start();
    require_once($dir.'/html.php');
    $html_message = ob_get_contents();
    ob_end_clean();

    // Load the SwiftMailer files
    require_once($dir.'/swift/swift_required.php');

    $mailer = new Swift_Mailer(new Swift_MailTransport()); // Create new instance of SwiftMailer

    $message = Swift_Message::newInstance()
                   ->setSubject('Patient Registration Form') // Message subject
                   ->setTo(array('info@hotmail.com' => 'Sam')) // Array of people to send to
                   ->setFrom(array('no-reply@net.tutsplus.com' => 'PRF')) // From:
                   ->setBody($html_message, 'text/html') // Attach that HTML message from earlier
                   ->attach(Swift_Attachment::newInstance($pdf_content, 'reg_form.pdf', 'application/pdf')); // Attach the generated PDF from earlier

    // Send the email, and show user message
    if ($mailer->send($message))
        $success = true;
    else
        $error = true;

     }

 }
 ?>

您的安全服务器与非安全服务器是分开的,对吗?看起来安全服务器的 display_errors(在 PHP 配置中)可能设置为 "on"。因此,您会看到来自 PHP 的与代码通知和错误相关的消息。除非您在开发服务器上,否则您几乎应该始终禁用该设置以避免信息泄露。

在担心 dompdf 正在做什么之前,您应该解决配置问题并向您的代码添加一些异常处理 and/or 验证。

对您发布的内容的一些想法。

您通常在不考虑 foreach 循环中的数据类型的情况下进行验证。您至少应该检查变量是否可以被 strip_slashes() 解析,例如:

foreach ($_POST as $key => $val) {
  if (is_array($_POST[$key])) {
    // assuming this is an array of values
    $post->$key = array_map('trim', array_map('strip_slashes', $_POST[$key]));
  }
  else
  {
    $post->$key = trim(strip_tags($_POST[$key]));
  }
}

2 - 您正在尝试验证对象上可能存在或不存在的属性。由于您没有进行任何明确的分配,因此您需要在验证之前检查这些属性是否存在:

if (!property_exists($post, 'first_name') || !property_exists($post, 'last_name') || empty($post->first_name) || empty($post->last_name))
{
  $error = true;
}

3 - 为什么首先要将属性分配给通用对象?如果你想使用一个对象,你可能应该继续定义 class 以便你可以在实例化期间填充和验证值。如果您不想这样做,请将它们分配给变量,这样您就可以知道,真正知道,您拥有什么数据。如果你想走捷径,你可以这样做(或自己写):

$first_name = filter_var($_POST['first_name'], FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);

为了对大多数人简化这一点,您甚至可以设置一个预期参数数组并解析该数组以设置您的值:

$post = array();
foreach (array('first_name','last_name') as $key)
{
  $post[$key] = filter_var($_POST[$key], FILTER_SANITIZE_STRING, FILTER_NULL_ON_FAILURE);
}

4 - 为清楚起见,使用花括号...总是

你可能想把这个交给 https://codereview.stackexchange.com/ 以获得更彻底的评论。