session_start()在任何 PHP 网页的开头使用“ ”函数,就在第一个 PHP 开始标记 ( <?php) 之后。
然后将您的变量存储到超全局会话数组变量中,在“first.php”页面中,例如:-
<?php
session_start(); // This line must be at the very beginning of this PHP page.
function one() {
// blah, blah, ...
if(isset($variable) && !empty($variable)) {
$_SESSION['customVariable'] = $variable;
}
// some more blah, blah, ...
}
?>
Run Code Online (Sandbox Code Playgroud)
现在,如果您来到“second.php”页面,您需要访问该页面的功能:-
<?php
function two() {
// if any blah, blah, ...
if(isset($_SESSION['customVariable']) && !empty($_SESSION['customVariable'])) {
$variable = $_SESSION['customVariable'];
}
// next series of blah, blah, ...
}
?>
Run Code Online (Sandbox Code Playgroud)
但在这个“second.php”页面中,“ session_start()”函数必须写在该页面的最开头,紧接在第一个 PHP 开始标记之后。
希望能帮助到你。