Php 根据登录用户名更改表单

Php 根据登录用户名更改表单,php,session,Php,Session,在我的主页上,我有一个登录表单。是否可以根据已登录的用户名将其更改为其他内容 比如说 <form id="form1" name="form1" method="post" action="testconnect.php"> <p> <label for="username">Username:</label> <input type="text" name="username" id="username" />

在我的主页上,我有一个登录表单。是否可以根据已登录的用户名将其更改为其他内容

比如说

<form id="form1" name="form1" method="post" action="testconnect.php">
  <p>
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="password">Password:</label>
    <input type="text" name="password" id="password" />
  </p>
  <p>
    <input type="submit" name="login" id="login" value="Submit" />
  </p>
</form>


用户名:

密码:

可以在主页上更改此选项以显示

欢迎回到“用户名”。注销


将登录数据放入
$\u COOKIE
中,如果发现COOKIE echo'Hello username',则回显您的表单

因此,如果我理解正确,您是否希望在用户已登录时隐藏表单,并在消息中显示其用户名?是的,你能做到。如果使用PHP会话存储用户数据,可以执行以下操作:

<?php
// Make sure to start a session first
if(!session_id()) {
    session_start();
}

// If a username is set, display a welcome message.
if(isset($_SESSION['username'])) {
    echo "Welcome back " . $_SESSION['username'];
} else {
    // No login session found, output the form here instead
?>
<form id="form1" name="form1" method="post" action="testconnect.php">
  <p>
    <label for="username">Username:</label>
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="password">Password:</label>
    <input type="text" name="password" id="password" />
  </p>
  <p>
    <input type="submit" name="login" id="login" value="Submit" />
  </p>
</form>
<?php
}


用户名:

密码:


你能晚点再试试吗?在问任何问题之前,先搜索它。我更喜欢会话而不是cookie。原则是一样的,不管shuffle 1990喜欢什么:)我更喜欢:会话+Web存储