有许多不同的方法可以做到这个,我会和你分享2。
INCLUDE
可以在事实上include()它使用第三文件,但我不建议作为会议是容易得多。但是,如果你想要一个配置类型文件,这也很有用。
顶部的login.php的:
include('global.php');
新文件:global.php candidate.php的
if (session_status() == PHP_SESSION_NONE) {
session_start();
} //start sessions if it isn't already started.
if(isSet($_POST['email'])) { //Check if email variable is POSTED
$email = $_POST['email']; //Set session variable "Email" to the posted data.
}
顶部
include('global.php');
你要包括全球文件在写入任何包含$ email的文件之前。这种方法的另一个亮点是你只需要一次启动会话(如果你在全局文件中启动它们),因为当你将它包含在其他页面上时,它会自行处理。
SESSIONS
在您要访问一个全局变量,你可以使用任何会话页面的顶部。
顶部的login.php的:
if (session_status() == PHP_SESSION_NONE) {
session_start();
} //start sessions if it isn't already started.
if(isSet($_POST['email'])) { //Check if email variable is POSTED
$_SESSION['email'] = $_POST['email']; //Set session variable "Email" to the posted data.
}
上Candidate.php:
if (session_status() == PHP_SESSION_NONE) {
session_start();
} //start sessions if it isn't already started.
echo $_SESSION['email']; //You can call this variable on any page that has sessions started
希望我帮你!