On Thu, 2010-07-15 at 15:38 +0000, Carlos Sura wrote:
> Hello mates, I'm developing a user registration and access level system...
> And I wonder... Is there any way to avoid to put code in every header page?
> <html> tags... And its annoying to look at this: Warning: session_start():
> Cannot send session cache limiter - headers already sent
> So, I'm wondering, is there any other way to avoid put code in every page?
> or... another way to avoid that kind of error.
> Thank you.
> Got a cool Hotmail story? Tell us now
A key concept to remember is that HTML is inserted into your PHP code,
and not the other way around. Once I realised the difference, I found I
was making the header/output mistake far less often.
Anyway, to answer the question, what most apps/websites do for this is
to use controller code to load in the correct HTML as necessary. The
first lines of most of my apps are general include lines. One for DB,
one for other config, etc. That way, I can just use the include files
for doing things that need to be done for every page.
Common logic for a login is to use an include file that does this:
1. Is user logged in? Yes: goto 5. No: goto 2
2. Have login details been submitted through form or other? Yes:
goto 3. No: goto 4
3. Are login details correct? Yes: goto 5, No: goto 4
4. Show login form & stop
5. Show/redirect to app page
(apologies for the hard to follow list, but I just realised I don't know
a good way to show a flowchart in plain text!)
Use include files for your HTML headers, and only include them after
you've done everything you need to with session_start() and header()
calls. If there's content that changes in the header from page to page,
put that in a variable that you use in the included file.