I know I should know the answer to this, but I'm drawing a blank...

I have a conditional statement that checks for the value of a variable, and if it isn't 'Y', then it does a redirect using the header statement.

apparently something that I did a couple days ago had a negative effect on the redirection, but I don't know what it would have been... I did a lot of minor updates to several scripts that could have an effect, but not sure what would stop the header redirection from working...

<?php
include "all_inc/config_site.php";
include "all_inc/pghead.php";
include "all_inc/pgtop.php";
include "all_inc/logcheck.php";  // include on all member pages
if ($accept != 'Y'){ // variable set in logcheck
header('Location:../memupdate.php');// if haven't acceptes TOS go there
exit;
}

A member just brought it to my attention that something was different, and I can see that it isn't forcing the redirect to the memupdate.php as it should be... But it is hitting the exit; which basically leaves me with the left menu and a blank page other than that.

So, I guess the question is ...
What would prevent the redirection from working?

I know there are certain circumstances where it won't work, but don't remember what exactly it is...

Thanks in advance.

Douglas

Dani AI

Generated

Short summary: when a PHP redirect stops working but your script still reaches exit, the usual cause is that headers were already sent before the script attempted to set the Location header. Since this page pulls in several include files early, the offending output is almost always coming from one of those files (stray whitespace, a UTF‑8 BOM, an echoed debug line, an HTML template included too early, or a PHP warning/notice). and were on the right track — this is a headers-before-output issue — but the fastest way to locate the exact file/line is a small diagnostic.

Quick diagnostic to run (place temporarily near the top of the page):

<?php
if (headers_sent($file, $line)) {
    printf("Headers already sent in %s on line %d\n", $file, $line);
    exit;
}

Practical next steps

  • Run the snippet above to get the file and line that first sent output. Fix that file (remove stray whitespace, remove/relocate any HTML, or remove debug echo/var_dump calls).
  • Check for UTF-8 BOMs — save PHP includes as “UTF-8 without BOM.” In many editors you can re-save encoding or use a hex viewer to confirm the first bytes.
  • Search included files for a closing ?> followed by a newline at the end of the file; for pure-PHP files, omit the trailing ?>.
  • Use browser DevTools Network or curl -I http://your/site/page.php to inspect whether a Location header is present.
  • As a temporary aid, ob_start() at the very top will hide early output so you can continue testing, but don’t rely on it as a permanent fix.

If the diagnostic reports an include you didn’t expect, comment includes in/out to isolate the culprit, fix that file, then retest. This approach finds the invisible output quickly and restores reliable redirects.

Recommended Answers

All 3 Replies

Member Avatar for Member #120589

Any output, e.g. echo or error before the header. Also are you sure that the location is correct. The memupdate.php file is a directory above this page?

Are you including an html file with the <html><head></head> template?
If so, that could throw it off because you can't release the html header before the header() function in PHP. You should do your PHP at the very top of the page before all those includes (unless it's your database connection one).
I'm assuming logincheck is with the variable you need? You can do that before. But you should always release your html header after using header() functions in php.

Any output, e.g. echo or error before the header. Also are you sure that the location is correct. The memupdate.php file is a directory above this page?

Yep. it has been working just fine up until something that I changed in one of the other scripts. Hate it when I do that, and get sidetracked and don't take the time to test things from all angles when I make changes.

And yes, that was the answer I was looking for... I knew it was something simple, but since I hadn't run into it in quite some time, had forgotten...

Thanks, I'm sure I will get it resolved now, based on that answer...

Have a great evening.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.