PHP $ _SESSION空值

时间:2017-10-10 06:51:41

标签: php session

我想在$new_client中显示preview.php变量,但我没有得到任何值,如果我将任何其他变量设置为klmn会话,那么它可以正常工作。有什么问题?

pdf/index.php

<?php
session_start();
if(!isset($_SERVER['HTTP_REFERER'])){
    header('location:../error.php');
    exit;
};

include("add_client.php");
$_SESSION['klnm']=$new_client;
echo $_SESSION['klnm'];

pdf/add_client.php

<?php


if(!isset($_SERVER['HTTP_REFERER'])){
    header('location:../error.php');
    exit;
};
require("../config.php");


$new_client = strtolower($_POST['new-offer']);

$_SESSION['klnm']=$new_client;

pdf/preview.php

<?php
session_start();

$klient = $_SESSION['klnm'];
echo $_SESSION['klnm'];

我的表格kokpit / index.php

<form method="post" action="../PDF/index.php">
                    <input type="text" name="new-offer">
                    <button type="submit">Wygeneruj</button>
            </form> 

1 个答案:

答案 0 :(得分:0)

你没有<button 'type'=submit 'name'=submit ..&gt;测试isset($ _ POST [&#39; submit&#39;])等待用户输入...它还有助于测试会话是否已经启动(参见下面的代码)以避免通知/不可预见行为:

的index.php:

 <?php
    session_start();

    echo 'in INDEX' . '<br />';

    if(!isset($_SERVER['HTTP_REFERER'])){
        header('location:../error.php');
        exit;
    };

    include("add_client.php");

    if(isset($_POST['submit']))
    {
    $_SESSION['klnm']=$new_client;
    echo '<br />' . 'INDEX output - $SESSION[klnm]: ' . $_SESSION['klnm'];

    echo "<br /> <br /> <a href='preview.php' > go preview </a>";
    }

    ?>

<form method="post" action="index.php">
        <input type="text" name="new-offer">
        <button type="submit" name='submit' >Wygeneruj</button>
        </form>

add_client.php:

<?php
if(!isset($_SERVER['HTTP_REFERER'])){
    header('location:../error.php');
    exit;
};

//require("../config.php");
if(isset($_POST['submit']))
{
echo '<br />' . 'in ADD_CLIENT, adding the client ' . '<br />';

$new_client = strtolower($_POST['new-offer']);

$_SESSION['klnm']=$new_client;
}

preview.php:

<?php
// check if session already started
if(!isset($_SESSION))
{
    session_start();
}

echo '<br />' . 'in PREVIEW' . '<br />';

$klient = $_SESSION['klnm'];
echo '$SESSION[klnm] == ' . $_SESSION['klnm'];

echo "<br /> <br /> <a href='index.php' > back to index </a>";
?>