0

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

Wallace Stev
  • 194
  • 1
  • 8

2 Answers2

1

You have a redirect header ("Location: error.php") , so actually you are doing two http requests => two different php processes(executed pages), the variable will not be visible. The easiest way is to store the value of the variable in the $_SESSION in the first page, and to read it in the second page. See PHP Pass variable to next page

You should also look to https://www.php.net/manual/en/reserved.variables.session.php

Marcel Preda
  • 1,045
  • 4
  • 18
  • Thank you for letting me know where I went wrong with my code. At first I was a bit skeptical about doing this with sessions since I'm already using sessions to maintain various aspects of user activities. So I tried the other way that @Progrock suggested first but then I had to move from that and go with sessions. Got it all figured out now and it works perfectly. Thanks! – Wallace Stev Jan 28 '21 at 12:51
  • @wallace_stev, if you already have already used the $_SESSION array, then it is even better: your effort was minimal. – Marcel Preda Jan 29 '21 at 18:37
1

You can add your error, code and/or message as a query string for the redirect.

<?php
$qs = http_build_query(
    [
        'error'    => true,
        'errorMsg' => 'Email already exists'
    ]
);
var_dump($qs);

Output:

string(37) "error=1&errorMsg=Email+already+exists"

Attach the query string to your redirect location.

Example of use of a code parameter for a path like: error.php?code=23. On error.php:

<?php
$error_codes = [
    '23' => 'Email already exists.'
];

if(isset($_GET['code']) && isset($error_codes[$_GET['code']])){
    echo $error_codes[$_GET['code']];
}
Progrock
  • 7,373
  • 1
  • 19
  • 25
  • I tried this way and it definitely works but then the URL gets all messy displaying all the info that you passed there. I appreciate your assistance. – Wallace Stev Jan 28 '21 at 12:45
  • @wallace_stev , just pass an error code, and use a lookup table to get your message. People generally don't care about URLs. I'd favour this method to unnecessary use of session storage. – Progrock Jan 28 '21 at 12:48
  • so that means I'd have to implement something like and array or similar structure with key value pairs and use the error code to lookup the messages or would it have to be stored in a database? – Wallace Stev Jan 28 '21 at 12:58
  • @wallace_stev a simple array would be fine. I've added an example. But... note that it may be inconvenient for someone to return after a redirect. – Progrock Jan 28 '21 at 13:25
  • Thanks, this seems much more efficient now. I tried it and it works perfectly. – Wallace Stev Jan 28 '21 at 14:29