如何在 php 中包含函数后检索变量

How to retrieve variables after include function in php

我已经使用 include 函数向页面添加了用户限制,但是这个操作清空了我的变量,因此登录后,结果不会因此显示,我怎样才能将变量保留在脚本中?我以为session是方法,结果不行

<?php
$trim=$_POST['trim'];
$estu=$_POST['estu'];
session_start();
$_SESSION['trim'] = $trim;
$_SESSION['estu'] = $estu;

if($trim == "RJ") {
echo "<h2>Login required!</h2>";
include ("../admin/accesusers.php");
}

$trim = $_SESSION['trim'];
$estu = $_SESSION['estu'];
echo "Your choices are ".$trim." and ".$estu; 
?>

所以最后一个 echo 的输出是... 您的选择是 and ...因为当 $trim == "RJ" 其他结果很好时不会检索变量,例如 你的选择是T13323

文件accesusers.php:

<?php
$db = new mysqli('localhost','usr','pass','Dbase');
include_once("login.php");
$_auth = new auth($db); // Anything past here is logged in
$username = $_auth->getUsername();
?>

login.php文件中,唯一提到SESSION的变量就是这个,难道是这个把有事吗?

define('LOGIN_SESSION_EXPIRED',-8);
....
LOGIN_SESSION_EXPIRED=>'<br /><h3>Your session is outdated!</h3>',
....
die($this->form(LOGIN_SESSION_EXPIRED,'signin'));

好的,我终于解决了。我已经意识到,只有当 include()if 内部时,变量才会丢失,而且 $_SESSION 必须从 $_POST 而不是从变量 ($trim) 来传播这个值;所以我用这种方式做了一个解决方法:

文件1.php:

<?php
$trim = $_POST['trim'];
$estu = $_POST['estu'];
if($trim == 'RJ') {
        header( 'Location: 2.php' ) ;
        session_start();
$_SESSION['estu'] = $_POST['estu'];
$_SESSION['trim'] = $_POST['trim'];
} else {
echo "<p>Your choices are ".$trim." and ".$estu."</p>";
}
?>

文件2.php:

<?php
session_start();
$estu = $_SESSION['estu'];
$trim = $_SESSION['trim'];
echo "<h2>Login required!</h2>";
include ("../admin/accesusers.php");
echo "<p>Your choices are ".$trim." and ".$estu."</p>";
?>

所以现在输出是“你的选择是RJ3323