drpp5680 2013-05-13 18:41 采纳率: 0%
浏览 62

在页面之间传递$ _SESSION变量

Edit: the session_start() was in the "entire code of initial page," but wasn't showing. I needed another newline.

Edit 2: Showing the output of the raw HTML rather than the displayed page.

I am working on a document conversion site. It takes a one or two page .docx file and converts it to simple html. (Basically replacing newlines with p tags, no html or head tags as it will be displayed in an existing page). For test purposes I'm converting a .docx file with a single word - "one" - in it.

I am storing the converted data as a string in the variable $_SESSSION['doc']. Because of the issues I've been having, I'm also storing a $_SESSION['test'] string.

I do a var_dump($_SESSION) on the initial page and I can see both variables with populated strings.

I do a var_dump($_SESSION) on the following screen, and the $_SESSION['test'] string comes through okay, but the $_SESSION['doc'] is now a 0 length string. I'm not certain what's truncating my 'doc' string.

The relevant variables are $_SESSION['doc'], $_SESSION['test'], and $outputData. I included the entire first page at the end of this post since I suspect $outputData is getting overwritten.

I believe this is the relevant code snippet for the initial page:

<?php $outputData = preg_replace("/.+/", "<p class='converted'>$0</p>", $outputData);
                              $outputData = preg_replace("/<\/p>

/", "<br /><br /></p>", $outputData); ?>
                <?php if(isset($outputData)){$_SESSION['doc'] = $outputData;}else{$_SESSION['doc']="JustATest";} $_SESSION['test'] = "<>()!';!SDFSDFG^%$"; echo "
<div><br /><br />var_dump: " . var_dump($_SESSION) . "</div>"; ?>

Output:

array(2) {
["doc"]=>
string(32) "<p class='converted'>one</p>
"
["test"]=>
string(18) "<>()!';!SDFSDFG^%$"

Relevant code on the follow up page:

<?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">

Output of follow up page:

session_id: jl40cdmbdpd3h40qg3ogmghkp0
OUTPUT: array(2) { ["doc"]=> string(0) "" ["test"]=> string(18) "<>()!';!SDFSDFG^%$" }

EDIT: I wasn't viewing the raw HTML. /me shakes head.

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.

Entire code for the initial page:

<?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>

/", "<br /><br /></p>", $outputData); ?>
                <?php if(isset($outputData)){$_SESSION['doc'] = $outputData;}else{$_SESSION['doc']="JustATest";} $_SESSION['test'] = "<>()!';!SDFSDFG^%$"; echo "
<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']); ?>
  • 写回答

2条回答 默认 最新

  • dtv11049 2013-05-13 18:55
    关注

    You must add session_start() to the top of all pages before anything is rendered as output to the page.

    Source

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用