php getting my butt kicked by headers :(

Joined
12 Feb 2006
Posts
17,633
Location
Surrey
unfortunately once again i'm having to turn to my last resort to get this sorted.

simple put i have a site that is created dynamically using php, ie. the page title includes a file which checks the file name and echos whatever is correct for the page the user is on, making it easier for me to change the titles if i need to in one big go, and also this is for coursework so i'm trying to do as much as possible with php. Other things are like lists of links echoed out depending on page, the stylesheet again includes a file and then php checks if a cookie is set, or if the user is logged in then what stylesheet they have chosen which is stored in a database, if not give the default stylesheet name.

now problem i'm having is with the login script and headers.

i have it all working on it's own (just php no html) but it's when i start including the login/logout/register pages with the design that i have problems with headers.

after googling i'm pretty certain the problem is that i have the page echo stuff like the stylesheet, site title, links, before it then changes the header, which is apparently not do able.

after changing a lot most of the log in/register scripts works, the only part i'm stuck with is after the user has logged in, if they revisit the login page (should not be able to but could happen accidentally) then the page checks cookies if they have logged in and then tries to redirect to the profile page which it can't because stuff is echoed out before.

Now i could just ignore it and remove the check if logged in part so making it possible for a logged in user to log in again, but the trouble is the plans i have for this site i will definately come across this problem again so really could do with a solution.

so any one able to help me out with this one?

[edit]
love it how everytime i have a problem, my first solution is to keep retrying it remembering why, if not check php/google, finally after many hours/days of searching i will turn to you guys, write a long post, then as i'm rereading my post to make sure it explains the problem i think of a fix.

ave realised i could just do the check if logged in at the top of the design page and then have the html/design, and then include the loggin script if still needed, so that way the first thing is the header if logged in.

ok this should sort the problem and hopefully any in future but if there is another solution i'd love to hear it.
 
Last edited:
I have a Document and Tag class which loosely incorporates psyreen's DOM class - it basically allows you to generate HTML programmatically and only output it when you're done. This has loads of benefits, least of all being that you can redirect someone to a different page if they're not logged in (not something I personally do, but whatever floats your boat) as you don't have any browser output until your html generation stops (I have my document destructor set to echo dom->SaveXML())

Cool thing about that is it makes it easy to integrate caching and all that jazz, as well as getting really close to the end of executing a script and changing what's in your head tags or something like that.
 
I like to dump straight to the screen wherever possible - I avoid putting HTML into variables then printing the variables whenever possible. Anything to do with headers is done before I output anything - e.g. check they're logged in before you even send <html>.


(edit; useless post, really... there is no "point" before anyone asks what my point is ;) just thought I'd say it)
 
thanks for that, does sound very useful so will have a look at it in more depth once i have current problem sorted

least of all being that you can redirect someone to a different page if they're not logged in (not something I personally do, but whatever floats your boat).

what better way would you suggest? i only have done it this way because i try not to look at other scripts as i then end up just wanting to use what they have written as that becomes the only way i can think to do it and this makes sense so far but obviously there is better (easier?) ways to do it.

I'm not after any code just if you could describe better ways of doing it?
 
I operate on all of my HTML being generated using separate classes, so I check whether someone's logged in (if logging in is restricted), and if they're not logged in, I instantiate the "not logged in" class, and generate the page that way. This means that you don't have to worry about keeping where your user is trying to access in a session or something as they're already there, just seeing the logged out version of the page.
 
Back
Top Bottom