Newbie coder here with another odd problem. I have a redirect that only works sometimes, but most frequently it does not.
<?php
// Redirect to Login if not logged in
if ($mg_login_status != "approved" OR empty($mg_user_name)) {
header("Location: https://www.myurl.com/login.php");
die();
}
?>
I do have this code placed prior to my html tag.
I know it is the above code that is the issue, as when I put in a testing code (echo ‘testing’) it does show that, but does not do the redirect.
I thought it might be a browser issue, but I notice the same problem in Chrome on my PC and Safari on my iPad.
No error message appears on screen (other than my test message) – it just doesn’t do the redirect.
I have read some posts on others having similar problems, and the recommendation is to use a Javascript redirect instead. Is this a better way of resolving this? (It sounds like it is).
However I notice that there are 2 methods described:
I’m not very familiar with Javascript at all, so I don’t fully understand whether it would be better to use “document.location” or “window.location.href”. I am just referring to a page on the same website as my other page. Is there a “more correct” one to use or are both perfectly fine?
<?php
// Redirect to Login if not logged in
if ($mg_login_status != "approved" OR empty($mg_user_name)) {
header("Location: https://www.myurl.com/login.php");
die('Header did not work!');
}
else{ die('If did not work!'); }
?>
It will at least confir whish part of the above code is not working as you expect.
It has be be before any output to the browser, that includes your DOCTYPE before the html tag and any whitespace, like a newline or space outside of the PHP tags.
To temporarily override the server settings add these statements to the top of the page:
<?php
declare(strict_types=1);
error_reporting(-1); // maximum errors
ini_set('display_errors', '1'); // show on screen
// Redirect to Login if not logged in
if ($mg_login_status != "approved" OR empty($mg_user_name)) {
header("Location: https://www.myurl.com/login.php");
die();
}
Edit:
Amended incorrect error-log to error_log and supplied link.