From: Carlos Sura on

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?
Because, almost every page contains javascript, so almost every page cotains <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
http://clk.atdmt.com/UKM/go/195013117/direct/01/
From: Richard Quadling on
On 15 July 2010 16:38, Carlos Sura <carlos_sura(a)hotmail.com> 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?
> Because, almost every page contains javascript, so almost every page cotains <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.

The quick and dirty way is to use output buffering - [1].

The better way is to use includes/requires - [2]. This will involve
moving code around the system, extracting common parts and then
"include"-ing them in the right place.

Also, a common practise is to only have output generated at the end of
the script, rather than using a lot of echo's through out the code. A
sort of manual output buffering.

Regards,

Richard Quadling.

[1] http://docs.php.net/manual/en/book.outcontrol.php
[2] http://docs.php.net/manual/en/language.control-structures.php and
look for include/require/include_once/require_once
From: kranthi on
i prefer using a template engine like smarty http://www.smarty.net/
From: Ashley Sheridan on
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?
> Because, almost every page contains javascript, so almost every page cotains <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
> http://clk.atdmt.com/UKM/go/195013117/direct/01/


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.

Thanks,
Ash
http://www.ashleysheridan.co.uk


From: Carlos Sura on

Hello Richard, Thank you for your answer, I'm looking now...

Kranthi, seems to be a pretty good solution, I will give it a try. Thank you.







> From: kranthi117(a)gmail.com
> Date: Thu, 15 Jul 2010 21:24:15 +0530
> To: RQuadling(a)googlemail.com
> CC: carlos_sura(a)hotmail.com; php-general(a)lists.php.net
> Subject: Re: [PHP] user login and access + headers already sent
>
> i prefer using a template engine like smarty http://www.smarty.net/
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

_________________________________________________________________
Do you have a story that started on Hotmail? Tell us now
http://clk.atdmt.com/UKM/go/195013117/direct/01/