如何将用户名变量传递给第三个php页面?

时间:2017-05-09 14:49:32

标签: php mysql forms session post

  1. 用户使用任何用户名和密码(index.php)登录
  2. 该表格将在" login.php"
  3. 处理
  4. 登录后," login.php"将用户引导至" friends.php"
  5. 我想在friends.php页面上显示用户最初使用索引脚本登录时输入的确切用户名

    我尝试使用session和$ _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'];
    
    ?>
    

4 个答案:

答案 0 :(得分:0)

在friends.php页面上的

执行类似以下的操作

{{1}}

我还建议使用此link

来使用w3 html验证程序

答案 1 :(得分:0)

您需要使用Session

类似的东西:

表格

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

Page friends.php

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

&GT;

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

答案 2 :(得分:0)

首先,你的代码会给你未定义的变量错误,从我看到没有使用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()并在查询中使用预准备语句。

祝你好运。

答案 3 :(得分:0)

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

相关问题