我使用dompdf创建了一个表单,当我将其上传到网站所属的常规服务器时,它运行正常。但是,当我将它移动到安全服务器(ssl)时,它没有工作,也没有发送表单。服务器运行Linux。网络服务器是带有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;
}
}
?>
答案 0 :(得分:0)
您的安全服务器与非安全服务器是分开的,对吗?看起来好像display_errors
(在PHP配置中)设置为&#34; on&#34;为安全的服务器。因此,您可以看到PHP中与代码通知和错误相关的消息。除非您使用开发服务器,否则您应该始终禁用该设置以避免信息泄露。
在您担心dompdf正在做什么之前,您应该解决配置问题并为您的代码添加一些异常处理和/或验证。
关于你发布的内容的一些想法。
您通常会在不考虑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 - 为什么要首先将属性分配给通用对象?如果要使用对象,则应该继续并定义类,以便在实例化期间填充和验证值。如果您不想这样做,请将它们分配给变量,以便您知道,真的知道,您拥有哪些数据。如果你想缩短它,你可以做这样的事情(或写自己的):
$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 - 为了清晰起见使用花括号......总是
您可能希望将其转到http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/DrawRectangle.htm以获得更彻底的批评。