编辑: session_start() 在“初始页面的整个代码”中,但没有显示。我需要另一个换行符。
编辑 2:显示原始 HTML 的输出而不是显示的页面。
我正在开发一个文档转换网站。它需要一页或两页的 .docx 文件并将其转换为简单的 html。(基本上用 p 标签替换换行符,没有 html 或 head 标签,因为它将显示在现有页面中)。出于测试目的,我正在转换一个 .docx 文件,其中包含一个单词 - “one”。
我将转换后的数据作为字符串存储在变量 $_SESSSION['doc'] 中。由于我遇到的问题,我还存储了一个 $_SESSION['test'] 字符串。
我在初始页面上做了一个 var_dump($_SESSION) ,我可以看到两个变量都带有填充的字符串。
我在下面的屏幕上执行了 var_dump($_SESSION) ,并且 $_SESSION['test'] 字符串可以正常通过,但是 $_SESSION['doc'] 现在是长度为 0 的字符串。我不确定是什么截断了我的“doc”字符串。
相关变量是 $_SESSION['doc']、$_SESSION['test'] 和 $outputData。因为我怀疑 $outputData 被覆盖了,所以我在这篇文章的末尾包含了整个第一页。
我相信这是初始页面的相关代码片段:
<?php $outputData = preg_replace("/.+/", "<p class='converted'>$0</p>", $outputData);
$outputData = preg_replace("/<\/p>\n\n/", "<br /><br /></p>", $outputData); ?>
<?php if(isset($outputData)){$_SESSION['doc'] = $outputData;}else{$_SESSION['doc']="JustATest";} $_SESSION['test'] = "<>()!';!SDFSDFG^%$"; echo "\n<div><br /><br />var_dump: " . var_dump($_SESSION) . "</div>"; ?>
输出:
array(2) {
["doc"]=>
string(32) "<p class='converted'>one</p>
"
["test"]=>
string(18) "<>()!';!SDFSDFG^%$"
后续页面相关代码:
<?php if(session_id() == ''){session_start();}
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $woocommerce;
$woocommerce->show_messages();
?>
<div>session_id: <?php echo session_id(); ?><br />OUTPUT: <?php echo var_dump($_SESSION); ?></div>
<?php do_action( 'woocommerce_before_cart' ); ?>
<form action="<?php echo esc_url( $woocommerce->cart->get_cart_url() ); ?>" method="post">
后续页面的输出:
session_id: jl40cdmbdpd3h40qg3ogmghkp0
OUTPUT: array(2) { ["doc"]=> string(0) "" ["test"]=> string(18) "<>()!';!SDFSDFG^%$" }
编辑:我没有查看原始 HTML。/我摇头。
IGNORE: One thing I found strange on the initial page is the single word "one" is showing up as a 32 char string. . . so there seems to be some extraneous white space after the document conversion. . . but it still outputs from the $_SESSION['doc'] variable on the initial screen.
初始页面的完整代码:
<?php if(session_id() == '' ){session_start();}
if(isset($_POST['added']))
{
add_basic_edit( $_POST['id'], $_POST['wc'] );
unset($_POST['added']);
header('Location: http://localhost/cart/');
exit;
}
/* Template Name: Upload Page */ get_header();
/**
* Test code to programmatically add a pricing calculator product to the cart, on
* every page load
*/
function add_basic_edit( $product_id, $num_words ) {
global $wc_measurement_price_calculator;
$product = get_product( $product_id ); // id of my 'word' product
$measurements = $wc_measurement_price_calculator->get_product_measurements( $product );
// get the one measurement
foreach ( $measurements as $measurement ) ;
$measurement->set_value( $num_words ); // the number of words
// add to cart
$wc_measurement_price_calculator->add_to_cart( $product->id, $measurement );
/var/www/html/websites/localhost/wordpress/wp-content/themes/Avada/upload.php}
if(isset($_POST['posted']))
{
require_once 'HTTP/Request2.php';
class DocumentConverterClient {
var $url = '';
function convert($inputFile, $outputType) {
$this->url="http://localhost:8080/jodconverter-sample-webapp-3.0-SNAPSHOT/converted/$inputFile.$outputType";
$request = new HTTP_Request2($this->url);
$request->setMethod(HTTP_Request2::METHOD_POST)
->setHeader('Content-Type', 'multipart/form-data')
->addPostParameter('outputFormat', $outputType)
->setBody($inputData);
$request->addUpload('inputDocument', $inputFile);
return $request->send()->getBody();
}
}
$documentConverter = new DocumentConverterClient();
$inputFile = $_FILES['inputDocument']['tmp_name'];
$outputType='txt';
$noext=current(explode(".", $_FILES["inputDocument"]["name"]));
$inputType=end(explode(".", $_FILES["inputDocument"]["name"]));
$outputFile = "data/$noext.$outputType";
$outputData = $documentConverter->convert($inputFile, $outputType);
//file_put_contents($outputFile, $outputData);
} ?>
<div class="column_group"><br /><br />
<div style="float:left;width:30%;" class="left_clumn">
<div style="float:left;width:35%">Wordcount:</div><div><?php $wc = str_word_count($outputData); echo $wc;?>
</div>
<div style="float:left;width:35%">Price Per Word:</div><div>$<?php $product_id=get_page_by_title('Basic Editing', 'OBJECT', 'Product' ); $product = get_product( $product_id->ID ); $ppw = $product->get_price(); echo $ppw;?>
</div>
<div style="float:left;width:35%">Total Price:</div><div>$<?php echo number_format(($wc * $ppw),2);?>
</div>
<div><br /><br />
<form name="add_to_cart" id="add_to_cart" action="../upload-page/" method="post">
<input type="hidden" name="added" value="added" />
<input type="hidden" name="id" value="<?php echo $product_id->ID; ?>" />
<input type="hidden" name="wc" value="<?php echo $wc; ?>" />
<?php $outputData = preg_replace("/.+/", "<p class='converted'>$0</p>", $outputData);
$outputData = preg_replace("/<\/p>\n\n/", "<br /><br /></p>", $outputData); ?>
<?php if(isset($outputData)){$_SESSION['doc'] = $outputData;}else{$_SESSION['doc']="JustATest";} $_SESSION['test'] = "<>()!';!SDFSDFG^%$"; echo "\n<div><br /><br />var_dump: " . var_dump($_SESSION) . "</div>"; ?>
<span class="BlueButton" onclick="document.getElementById('add_to_cart').submit()">Add to Cart</span>
</form>
</div>
</div>
</div>
<div style="float:right;width:70%;" class="right_column">
<div><?php echo $outputData; ?></div>
</div>
</div>
<div id="content" style="<?php echo $content_css; ?>">
</div>
<div id="sidebar" style="<?php echo $sidebar_css; ?>"><?php generated_dynamic_sidebar(); ?></div>
<?php get_footer(); unset($_POST['posted']); ?>