会话变量不会显示

时间:2015-03-24 15:18:15

标签: php forms session post

我正在尝试将POST数据发送到SESSION变量

$ _ SESSION ['plan'] = $ _POST ['plan']

info.php显示已加载会话。 浏览器 - > inspect元素似乎显示会话已初始化但没有key =>值。

但我无法显示会话值。 apache日志中没有错误。

主要动态框架:

cat index.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta name="robots" content="index,follow"/>
<link rel="icon" href="images/favicon.ico" type="image/x-icon">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<nav class="nav">
        <ul>
              BLAH
        </ul>
</nav>
<div id="content">
        <?php
                $pages_dir = 'pages';
                if (!empty($_GET['p'])) {
                        $pages = scandir($pages_dir, 0);
                        unset($pages[0], $pages[1]);
                        $p=$_GET['p'];
                        if(in_array($p.'.inc.php', $pages)){
                                include($pages_dir.'/'.$p.'.inc.php');
                        }else {
                                echo 'Sorry, page not found.';
                        }
                }else{
                        include($pages_dir.'/home.inc.php');
                }
        ?>

</div>
</body>

</html>

用户表单页面:

cat pages / payment.inc.php

<?php
//$_SESSION['plan'] = $_POST['plan'];
?>
<div id="content_pay">

<form action="pages/scheckout.php" method="post">
        <div>

                <input type="radio" id="plan1" name="plan" value="2500"> Beta membership <br><br>
                <input type="radio" id="plan2" name="plan" value="3500"> VIP membership <br><br>

                <label for="plan"> If you would like to pay another amount, enter the amount here:</label>
                <input type="text" id="plan3" name="plan" />
                <br>
                <label for="invoice_num"> Enter the invoice number here:</label>
                <input type="text" name="invoice_num" /> <br>
                <input type="submit" value="submit" name="submit">
        </div>
</form>


</div>

基本上这个页面只是为了捕获POST并将其分配给SESSION,然后在加载SESSION的情况下重定向:

cat pages / scheckout.php

<?php
session_start();

$_SESSION['plan'] = $_POST['plan'];

//needed to prevent weird race conditions
session_write_close();
header("location: ../index.php?p=scheckout");
die();

echo "<br>";
echo $_SESSION['plan'];

?>

去哪里:

cat pages / scheckout.inc.php

<?php

require_once('pages/sconfig.php');

?>

<div id="content_pay">

<form action="pages/scharge.php" method="post">
        <div>


        <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                data-key="<?php echo $stripe['publishable_key']; ?>"
                myElem.setAttribute('data-amount', <?php $_SESSION['plan']; ?>);
                myElem.setAttribute('data-description', <?php $_SESSION['plan']; ?>); >
        </script>
        </div>

</form>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>


<?php
echo $_SESSION['plan'];
?>

如何从POST加载SESSIONS以显示?

2 个答案:

答案 0 :(得分:1)

你必须在html输出之前只启动一次会话。会话保持打开状态,直到您使用session_destroyunset $_SESSION变量,或关闭浏览器。我建议您删除除index.php中的所有session_start以外的所有{{1}}。

答案 1 :(得分:0)

您使用SESSION [&#39;计划&#39;],但应使用echo SESSION [&#39; plan&#39;]

<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key="<?php echo $stripe['publishable_key']; ?>"
            myElem.setAttribute('data-amount', <?php echo($_SESSION['plan']); ?>);
            myElem.setAttribute('data-description', <?php echo($_SESSION['plan']); ?>); >
    </script>