php - Passing $_SESSION variable between pages -
edit: session_start() in "entire code of initial page," wasn't showing. needed newline.
edit 2: showing output of raw html rather displayed page.
i working on document conversion site. takes 1 or 2 page .docx file , converts simple html. (basically replacing newlines p tags, no html or head tags displayed in existing page). test purposes i'm converting .docx file single word - "one" - in it.
i storing converted data string in variable $_sesssion['doc']. because of issues i've been having, i'm storing $_session['test'] string.
i var_dump($_session) on initial page , can see both variables populated strings.
i var_dump($_session) on following screen, , $_session['test'] string comes through okay, $_session['doc'] 0 length string. i'm not what's truncating 'doc' string.
the relevant variables $_session['doc'], $_session['test'], , $outputdata. included entire first page @ end of post since suspect $outputdata getting overwritten.
i believe relevant code snippet 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>"; ?>
output:
array(2) { ["doc"]=> string(32) "<p class='converted'>one</p> " ["test"]=> string(18) "<>()!';!sdfsdfg^%$"
relevant code on follow 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 page:
session_id: jl40cdmbdpd3h40qg3ogmghkp0 output: array(2) { ["doc"]=> string(0) "" ["test"]=> string(18) "<>()!';!sdfsdfg^%$" }
edit: wasn't viewing raw html. /me shakes head.
ignore: 1 thing found strange on initial page single word "one" showing 32 char string. . . there seems extraneous white space after document conversion. . . still outputs $_session['doc'] variable on initial screen.
entire code 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 programmatically add pricing calculator product 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 'word' product $measurements = $wc_measurement_price_calculator->get_product_measurements( $product ); // 1 measurement foreach ( $measurements $measurement ) ; $measurement->set_value( $num_words ); // number of words // add 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 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']); ?>
you must add session_start()
top of pages before anything rendered output page.
Comments
Post a Comment