简体   繁体   English

从其他文件 php 获取变量的值

[英]Getting value of a variable from other file php

How will I get the value of variable username from login.php and pass to the query of get.php Login.php如何从 login.php 获取变量 username 的值并传递给 get.php Login.php的查询

get.php get.php

At the beginning of both files, you should start a session:在两个文件的开头,您应该启动一个 session:

<?php
session_start();
?>

Then you should put the username in a session, like shown below:然后你应该把用户名放在 session 中,如下所示:

<?php
if (!empty($_POST["username"]) {
    $_SESSION["username"] = $_POST["username"];
} else {
    echo "<p>You didn't fill in a username</p>";
}
?>

Then in the get.php file remove the "require_once login.php" line, and add the variable $username like so:然后在 get.php 文件中删除“require_once login.php”行,并添加变量 $username,如下所示:

<?php
$username = $_SESSION["username"];
$sql = "SELECT firstname, contactnum FROM tb_register  WHERE username= '".$username."'";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM