On one of my class-based sites, everything is controlled via the index.php
page, and every page is its own class
that gets called via that index page.
In one of those classes lies both the logged-in and logged-out header that gets called for every page of the site, both of which make a call for an Ad
that appears at the top of the site.
The problem is that whenever there is a redirect on a page, it doesn't happen until the class for that page gets called, which happens after the header has already been called, which means that an ad call was made for an ad that was never seen since the page ended in a redirect.
This negatively impacts the performance of the ads since it lowers the Click-through Rate (CTR) of the ads.
How can I ensure an Ad call is not made when a page ends up in a redirect, if there is no way to know the page ended up redirecting until the class for that page is loaded?
The only "outside-the-box" solution I can think of is to somehow use the output buffer to first load the class for that page, and then somehow append the header to the start of the buffer if it makes it to that point with no redirect, but I have no idea if that would work or how you would even do that.
Any ideas?
EDIT:
To clarify, the Redirects are already done via the HTTP header, and all output is currently being buffered. However, each page can have it's own reasons for redirecting, so that's why it's controlled by each page. For example, one page takes care of sending messages, and when a message is sent, it redirects the user back to their inbox after saving the message to the Database. On another page, logged-out users aren't allowed, so that particular page checks to see if a user is logged-in, and if not, redirects them to the login page.
However, it seems that even with full output buffering enabled and with the redirects being done via the header() function inside individual class pages, the buffering STILL does an Ad call before running the header redirect.
Redirects must happen in the HTTP header, not using HTML meta tags or Javascript trickery or similar. HTTP headers need to be output before anything else. Because of that and simply because it makes sense, your application should decide whether it wants to redirect the user or not before it even attempts to output any HTML. Therefore there should be no problem at all.
If your current application logic outputs or prepares HTML (which triggers ads) before it decides to redirect the user, you simply have a bad application structure. Change it. It should work like this:
...
if ($userNeedsToBeRedirected) {
header('Location: http://example.com/other/url');
exit;
}
// output HTML and ads here
// never triggered on a redirect