I've run into some difficulties with register_shutdown_function() in PHP. I have a header.php file which contains:
function shutdown(){
require '/includes/footer.php';
}
register_shutdown_function('shutdown');
//More code
extract($_SESSION);
$_SESSION contains an array called "user", containing things like the user ID and their username. I extract $_SESSION simply so I can use $user["id"] rather than $_SESSION["user"]["id"]. Then, in footer.php I have this code:
$_SESSION["user"]=$user;
to put the info back into the session, in case it's changed. But I'm getting the error:
Notice: Undefined variable: user in C:\xampp\htdocs\includes\footer.php
If I add print_r($user) to index.php then it prints it out perfectly. Similarly, if I remove the entire "shutdown" part from header.php, and then manually add:
require '/includes/footer.php';
to index.php then there's no issue.
So, if your register_shutdown_function() involves including a file (footer.php), should you be able to use variables from index.php in footer.php? If not, why is there no variable called $user?
Because the variable $user
doesn't exist inside the function's scope. You should be doing what your question says and passing it like a variable:
extract($_SESSION);
function shutdown($user){
echo $user;
require '/includes/footer.php';
}
register_shutdown_function('shutdown', $user);