Session works in one page but not another

别说谁变了你拦得住时间么 提交于 2019-12-11 06:08:50

问题


I am working in a php charting package. I start with a form.html that sends to an answer.php page. The answer.php creates the SESSION. Then a required test.php page picks up the session but the required graph.php page does not. I cannot transfer my data from the form to the graph. The required phplot.php is the graph engine and is not needed in the SESSION thread.

I have spent five days researching and testing with no luck. I have switched the coding around many times. I'm hoping that someone with great php experience will recognize something easily.

The coding for the three pages is below.

answer.php page:

<?php
session_unset();
session_start();
require_once 'phplot/test.php';
?>
<?php
$_POST['bs_now2'];$bs_now2 = $_POST['bs_now2'];$_SESSION['bs_now2'] = $bs_now2;
echo "<div id='graph'>";
echo "<p class='martop10 f18 b'>Graph:</p>";
echo "<img src='phplot/graph.php'>";
echo "</div>";
?>
<?php
session_destroy();
?>

graph.php page:

<?php
$Sbs_now2 == 0;
session_start();# Is this redundant? I've tried it in and out.
?>
<?php
require_once 'phplot.php';#Graph engine
$delta = 0.1;$sigma = 15;$sqrt2pi = sqrt(2*M_PI);$u = 75;
$data = array();
for ($x = 0; $x <= 150; $x++)
  $data[] = array('', $x, $Sbs_now2 + $x);
unset($Sbs_now2);
?>

test.php page:

<?php
#I don't need the session_start() for this page
$_POST['bs_now2'];$bs_now2 = $_POST['bs_now2'];$_SESSION['bs_now2'] = $bs_now2;$Sbs_now2 = $_SESSION['bs_now2'];
echo '<b>Session BS Now: '.$Sbs_now2.'</b><br>';
?>

Thank you,

Gary


回答1:


First thing In every page you will use "$_SESSION" you need to start session through "session_start()" if session start is not present in the code you can not get data from $ _SESSION variable, I recommend using $_POST variable Your php session is not full and does not cause problems

Use this: (Pass throught $_POST)

<?php
// passing data to the post
$_POST['data'] = "Test using Post";
// Receiving the data from the post
$data = $_POST['data'];
// to test and see if it works
echo $_POST['data']; // or echo $data

Hope i have helped

Cheers




回答2:


Hi Luiz Fernando Sousa Camargo and All,

With help from Luiz Fernando Sousa Camargo I have got it!

answer.php page has one change:

<?php
session_unset();
session_start();
require_once 'phplot/test1.php';
?>
<?php
$_POST['bs_now2'];$bs_now2 = $_POST['bs_now2'];$_SESSION['bs_now2'] = $bs_now2;$Sbs_now2 = $_SESSION['bs_now2'];
echo "<div id='graph'>";
echo "<p class='martop10 f18 b'>Graph:</p>";
echo "<img src='phplot/bs4c.php?bs_now2=$Sbs_now2'>";#CHANGE:
#I set bs_now2= to the session variable(as it would come from the form input). No other changes.
echo "</div>";
?>
<?php
session_destroy();
?>

graph.php page has a one important change:

<?php
$Sbs_now2 == 0;
session_start();
$_GET['bs_now2'];$bs_now2 = $_GET['bs_now2'];$_SESSION['bs_now2'] = $bs_now2;$Sbs_now2 = $_SESSION['bs_now2'];#CHANGE
#Use GET to acquire the session variable
#Remove End php and Start php Marks(not important)
require_once 'phplot.php';
$data = array();
for ($x = 0; $x <= 150; $x++)
  $data[] = array('', $x, $Sbs_now2 + $x);
unset($Sbs_now2);
?>

Thank you Luiz Fernando Sousa Camargo and All,

Gary



来源:https://stackoverflow.com/questions/43146310/session-works-in-one-page-but-not-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!