【问题标题】:How do I pass the username variable to a third php page?如何将用户名变量传递给第三个 php 页面?
【发布时间】:2025-12-13 14:20:03
【问题描述】:
  1. 用户使用任何用户名和密码 (index.php) 登录
  2. 表单在“login.php”中处理
  3. 登录后,“login.php”会将用户引导至“friends.php”

我想在friends.php页面上显示用户最初使用索引脚本登录时输入的确切用户名

我尝试使用会话和 $_POST,但没有成功。请帮忙!

index.php

<html>


    <body>
            Username: <input type = "text" name = "username"><br>
            Password: <input type = "password" name = "pass_word"><br>
            <input type = "submit" value = "Login"> 
        </form>

    </body>

</html>

<?php

    session_start();

    if (isset($_POST['login'])) {
        $_SESSION['username'] = $_POST['username'];
    }

?>

login.php

<?php

    session_start();

    if(isset($_SESSION['loggedin']) == true) {
        header("Location: friends.php");

    }

?>

friends.php

<?php

    session_start();

    echo $_POST['username'];

?>

【问题讨论】:

  • 将用户 ID 存储在会话中,不要忘记 session_start,使用存储在会话中的我从用户表中检索我要的行并显示用户名
  • echo $_SESSION['username'] in friends.php 而不是 echo $_POST['username'];
  • 建议使用&lt;form&gt;标签和method="post"

标签: php mysql forms session post


【解决方案1】:

首先,你的代码会给你未定义的变量错误,据我所知,没有带有 POST 方法的表单,也许你漏掉了那部分。

这是你需要做的

index.php

<?php
    session_start();

    if(isset($_SESSION['username']) && !empty($_SESSION['username'])){

        header("location:friends.php");
    }

?>
<html>
    <body>
        <form method="POST" action="login.php">
            Username: <input type = "text" name = "username"><br>
            Password: <input type = "password" name = "pass_word"><br>
            <input type = "submit" value = "Login" name="loginBtn"> 
        </form>
    </body>
</html>

我还可以看到您已标记mysql,但您没有任何标记 使用您的代码进行查询,因此您必须自己进行查询,只需 修正你现在的错误。

login.php

<?php
session_start();
$errors = "";
$fields = array("username","pass_word");


if (isset($_POST['loginBtn'])) {

    //check if fields are not empty
    foreach ($fields as $key => $fieldname) {

        if (!isset($_POST[$fieldname]) && empty($_POST[$fieldname])) {

            echo "please enter username and password";
            $errors++;
        }
    }

    if ($errors <= 0) {
        //we have no errors 

        $password = $_POST['pass_word'];
        $username = $_POST['username'];

        //THE DO YOUR QUERIES WHEN THEY ARE SUCCESSFUL THEN


        $_SESSION['username'] = $username;

        header("location:friends.php");  
    }

}
?>

friends.php

<?php

    session_start();

    if(isset($_SESSION['username']) && !empty($_SESSION['username'])){


        echo $_SESSION['username'];
    }else{

        // THE USER WAS NOT SUPPOSE TO BE HERE DO SOMETHING ABOUT THAT
    }
?>

注意:如果要将其存储在密码数据库中,则需要使用 password_hash()password_verify() 并在查询中使用准备好的语句。

祝你好运。

【讨论】:

  • session_start(); $password = $_POST['pass_word']; $username = $_POST['username']; $_SESSION['username'] = $username;
  • @Masivuye 如果我们将用户名放在提交按钮上会怎样?有可能吗?
【解决方案2】:

您可以设置会话变量,并在网页之间与会话共享变量。

【讨论】:

    【解决方案3】:

    你需要使用Session

    类似:

    表格

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

    页面friends.php

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

    ?>

    查看文档以获取更多信息:http://php.net/manual/en/session.examples.basic.php

    【讨论】:

      【解决方案4】:

      在 friends.php 页面上执行如下操作

      <?php 
      
      session_start()
      
      if(isset($_SESSION['username']){
          echo $_SESSION['username'];
      }
      else{
          echo 'username not set';
      }
      
      ?>
      

      我还建议使用 w3 html 验证器,使用 link

      【讨论】: