为什么这个简单的php会话没有';不起作用


Why this simple php session doesn't work?

我的会话机制似乎坏了,所以我尝试使用这段很短的代码。这里怎么了?我是不是错过了什么?

index.php

<?php session_start();?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <?php
        echo '<form action="update.php?'.  session_id().'" method="POST"><fieldset><input type="text" value="hello"/><input type="submit" value="OK"/></fieldset></form>';
        print_r($_SESSION);
    ?>
</body>
</html>

update.php

<?php
    session_start();
    print_r($_POST);
    foreach($_POST as $key => $value)
        $_SESSION[$key]=$value;
    session_write_close();
    print_r($_SESSION);
?>
<a href="index.php?<?php echo session_id();?>">hello</a>

如果你的帖子不起作用,很可能是因为你的from输入没有名称:

index.php

<?php session_start();?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
        <form action="update.php" method="POST">
                <fieldset>
                    <input type="hidden" name="session_key" value="<?php echo session_id(); ?>" />
                    <input type="text" name="message" value="hello"/>
                    <input name="submit" type="submit" value="OK"/>
                </fieldset>
        </form>
        <?php  print_r($_SESSION); ?>
</body>
</html>

update.php

    session_start();
    print_r($_POST);
    foreach($_POST as $key => $value)
        $_SESSION[$key] = $value;
    session_write_close();
    print_r($_SESSION);
<a href="index.php?id=<?php echo session_id();?>">hello</a>