I have two files, one named functions.php and another named error.php, what I want to do is run some procedures in functions.php, overwrite the error variables, call the error.php file and display the error there but I can't seem to get the scope of the variables right. I need some assistance and an explanation on what is wrong with my code.
In my functions.php file I have something like this:
<?php
$error = false;
$errorMsg = 'Some text';
function register ($email, $password) {
//Calling global error variables
global $error, $errorMsg;
//MySQL query to check if email exists in DB
if (/*email exists condition*/) {
//Overwrite error variables
$error = true;
$errorMsg = 'Email already exists';
//Redirect to error page
header ("Location: error.php");
}
else {
//Continue adding values to DB
}
} ?>
Then in my error.php file I have some shorthand PHP code like this:
<?php require_once (functions.php); ?>
<?php if($error): ?>
<h2>An error occurred: <?php echo $errorMsg; ?></h2>
<?php elseif(!$error): ?>
<h2>No error: All operations completed successfully</h2>
<?php else: ?>
<h2>An unknown error occurred</h2>
<?php endif; ?>
Now when I provide an entry that already exists in a database, the page redirects but it seems that the error variables are not updated and the newly-opened error.php page shows the output as:
No error: All operations completed successfully