似乎无法找到我的$ _POST出了什么问题

So I'm trying out sessions and this is how my code looks like:

session_id($username);
session_start();
$_SESSION['logged_in'] = true;
$_SESSION['time_logged_in'] = time();
$_SESSION['userName'] = $_POST['user[name]'];

My form looks like this:

<form method="POST" action="login.php"> 
    <table>
    <!--Text input for login-->
    <tr><td>Username:</td>
    <td><input type = 'TEXT'
           name = 'user[name]'
           value = '<?php  if(isset($_POST["user"])) {$tmp = $_POST["user"]; echo $tmp["name"]; } ?>'></td></tr>

I've tried a combination of $_POST['user'] and $_POST['user']['name']

error code: Notice: Undefined index: user[name] in C:\xampp\htdocs\Assignment_3\login.php on line 22

any help will be greatly appreciated.

$_POST is a map (associative array) that contains key/value strings - not objects, so lines like:

$tmp = $_POST["user"]; 
echo $tmp["name"];

and:

$_POST['user[name]'];

won't work.

Further, when you assign the name property with value like: user[name] - when the form is submitted it'll pass encoded : user%5Bname%5D - so don't do that: use simple names for the input fields in your HTML form.

You have to start your session before calling any session . Your page must start with session_start(); And you can't really mess up with code Like you did . Post more code please.

At a quick glance, it appears you mean to be using your session variables instead of post.

You have:

value = '<?php  if(isset($_POST["user"])) {$tmp = $_POST["user"]; echo $tmp["name"]; } ?>'></td></tr>

And it should be:

value = '<?php  if(isset($_SESSION["userName"])) {echo $_SESSION["userName"]; } else { echo ''; } ?>'></td></tr>