Passing $_SESSION variable between pages

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>\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) {
string(32) "<p class='converted'>one</p>
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;


<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();}

add_basic_edit( $_POST['id'], $_POST['wc'] );
header('Location: http://localhost/cart/');
/* 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 );

require_once 'HTTP/Request2.php';

class DocumentConverterClient {
    var $url = '';

    function convert($inputFile, $outputType) {
        $request = new HTTP_Request2($this->url);
            ->setHeader('Content-Type', 'multipart/form-data')
            ->addPostParameter('outputFormat', $outputType)
        $request->addUpload('inputDocument', $inputFile);

        return $request->send()->getBody();

$documentConverter = new DocumentConverterClient();

$inputFile = $_FILES['inputDocument']['tmp_name'];
$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 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 style="float:left;width:35%">Total Price:</div><div>$<?php echo number_format(($wc * $ppw),2);?>
        <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>
    <div style="float:right;width:70%;" class="right_column">
        <div><?php echo $outputData; ?></div>

<div id="content" style="<?php echo $content_css; ?>">
<div id="sidebar" style="<?php echo $sidebar_css; ?>"><?php generated_dynamic_sidebar(); ?></div>
<?php get_footer(); unset($_POST['posted']); ?>


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


This is a poor answer as I still don't know why $_SESSION['doc'] was getting overwritten, particularly since $_SESSION['test'] was retained. I think it has to do with my various if(isset()) statements for processing form data. I believe one of them was at some point overwritting $outputData. . . but I'm not certain where.

I changed the first such statement to grab outputData from the _POST variable:

    add_basic_edit( $_POST['id'], $_POST['wc'] );
    $_SESSION['doc'] = $_POST['outputData'];

I added a hidden input to the form, and a couple more regexes to get rid of single and double quotes:

$outputData = preg_replace("/\"/", "&quot;", $outputData);
$outputData = preg_replace("/\'/","&rsquo;", $outputData);

<input type="hidden" name="outputData" value="<?php echo $outputData; ?>" />

If anybody can point out where I'm messing up either the $_SESSION['doc'] variable, or where $outputData is getting overwritten this would be a better answer.

I did try making session_start(), without an if statement, the first thing in both of these pages, but it didn't make a difference.

Need Your Help

Ruby Regex Help

ruby regex

I want to Extract the Members Home sites links from a site.

Open-source java XSLT 2.0 implementation?

java xml open-source xslt-2.0 xslt

I'm currently looking into using XSLT 2.0, but I cannot find any open-source java implementations (Saxon-B seems to fit the bill, but isn't schema-aware).

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.