如何使变量可以在任何页面上访问? [重复]

This question already has an answer here:

I've made a login script (http://pastebin.com/bCpXYwPq), and I know it's prone to SQL injection, I'm fixing that later.

Anyway, I need the variable '$users', so I can echo it anywhere.

Example: On the account page, I want it to echo the email address. How would I do that if the variable is only stored in the login script?

</div>

Put the serialized variable $users on $_SESSION

like this:

session_start();
$_SESSION['users'] = serialize($users);

And to catch the $user again:

session_start();
$users = unserialize($_SESSION['users']);

You need to dig into sessions (http://php.net/manual/en/features.sessions.php)

login.php:

session_start();
$_SESSION["username"]=$username;
$_SESSION["email"]=$email;
$specialdata=array("a","b","c");
$_SESSION["specialdata"]=serialize($specialdata);

On all other pages you need access to that data:

session_start();
if(!isset($_SESSION["username"]))
{
// User not logged in
echo "You aren't logged in"
}
else
{
// Logged in
echo "The username is: " . $_SESSION["username"];
echo "Your email is: " . $_SESSION["email"];
$specialdata=unserialize($_SESSION["specialdata"]);
echo "First element of special data: " . $specialdata[0]; // Prints a
}

Sessions. definetly. Use session_start(); on all pages so the sessions are activated on the login page just declare the $_SESSION['username']; and you can use $_SESSION['username']; everywhere.