$ SESSIONS

时间:2016-11-19 11:47:59

标签: php session post

为什么它不起作用?

的index.php

<strong>Your name:</strong>
<form action="name.php" method"post">
  <input type="text" name="username"/>
  <input type="submit" name="Submit" value="Submit!" />
</form>
<?php 
  session_start();
  $_SESSION['post-data'] = $_POST;
?>

name.php

<strong>Your name is:</strong>
<strong>
  <?php 
    session_start();
    echo $_SESSION['post-data'];
  ?>
</strong>

name.php 打印我:

  

您的名字是:数组

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

首先,在加载任何HTML之前,session_start();应始终放在文档的顶部。

<强>的index.php

//No need to start session here as no $_SESSION variables needed. 

<strong>Your name:</strong>
<form action="name.php" method="post">
<input type="text" name="username"/>
<input type="submit" name="Submit" value="Submit!" />
</form>

其次,在将$_POST数据发布到name.php后分配,然后将其分配给$_SESSION

<强> name.php

<?php
session_start();
$_SESSION['username'] = $_POST['username'];
?>

<strong>Your name is:</strong>
<strong><?php echo $_SESSION['username'];?></strong>

如果您想查看数组中的内容以进行调试,则可以使用var_dump()

var_dump()

的示例
var_dump($_POST);

var_dump($_SESSION);
相关问题