dongxiang3648 2016-10-15 22:46
浏览 57

使用PHP传递会话变量的问题[重复]

I'm trying to understand session variables by trying this code. For the purpose of the test, I created 3 pages and tried to pass the session variables around. I noticed that, the session variable is not populated in the 3rd page. I have searched to see where the problem is and I'm yet to figure it out.

<kbd>page1</kbd>

<!DOCTYPE html>
<html lang="en">    
    <head>
        <meta charset="UTF-8">
        <title>Bio Data</title>
    </head> 
    <body>
        <form action="process.php" method="post">
            <label for="fst_name">
                First Name
                <input type="text" name="fst_name" id="fst_name">
            </label>

            <label for="lst_name">
                Last Name
                <input type="text" name="lst_name" id="lst_name">
            </label>

            <input name="submit" type="submit" value=" Login ">
        </form>
    </body>
</html>

<kbd>page2</kbd>

<?php
session_start();

if (isset($_POST["submit"])){           
    $_SESSION['fst_name'] = $_POST['fst_name'] ;
    $_SESSION['lst_name' ] = $_POST['lst_name'] ;
    header("location:display.php");
}else{
    echo "Error";
}

<kbd>page3</kbd>

<?php
session_start();
echo $_SESSION['fst_name'];
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Biodata Display</title>
    </head>
    <body>
        <table>
            <tr>
                <td><?php echo "Hello " .  $_SESSION['fst_name']; ?></td>
                <td><?php echo $_SESSION['lst_name']; ?></td>
            </tr>
        </table>    
    </body>
</html>
</div>
  • 写回答

1条回答 默认 最新

  • douyan3478 2016-10-15 23:16
    关注

    Sessions in PHP work by saving a data file server side referenced by a special cookie (by default named PHPSESSID), so when your browser sends the proper cookie PHP fetches from the saved data files the previous $_SESSION value. This means that:

    1. Your server must be able to write files (usually in tmp dir, check PHP INI settings related to session save path
    2. Your browser must accept cookies (you can easily check this by dumping $_COOKIE)

    To simplify your test, create a single page like this:

    <?php
    session_start();
    header("Content-Type: text/plain");
    var_dump($_COOKIE);
    var_dump($_SESSION);
    $_SESSION['hello'] = "world";
    

    After you load this page TWICE, you should see the right data in $_SESSION and $_COOKIE. If it doesn't work, your problem is in your server/environment. If that works, I'll look again at your code but it looked fine on first sight.

    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像