User validation pattern in PHP with Smarty -
i using php , smarty. have simple application that:
- shows page header
- checks if user logged in -> check session
- checks if user wants log out -> clear session
- if logged in shows menu.
- if not logged in, challenges user id/password -> set session
- shows page footer
now want add following header:
- username if logged in
- "login" string if not logged in
to seems require placement of printheader function become complicated. cannot print header, until know if user logged in/just logged in/has logged out, plus header has go before user/password challenge.
what approach can use here? possible buffer "not logged in" header, replace "logged if header" if required.
smartyapp::printheader(); application->isloggedin() application->doesuserwantstologout() application->ifnotloggedinthenshowchallengetouser() application->ifloggedinthenshowfullmenu() smartyapp::printfooter();
i think more convenient pattern incorporate conditions within templates themselves, rather keeping multiple templates each possible outcome. example:
$smarty->assign('user', new user($_session)); $smarty->display('index.tpl'); <!-- header.tpl --> {if $user->islogged()} <p>welcome {$username}</p> {else} <p>please log in</p> {/if} <!-- body.tpl --> {if $user->islogged()} {include file="menu.tpl"} {else} <p>you must signed in view menu</p> {/if} <!-- index.tpl --> {include file="header.tpl"} {include file="body.tpl"} {include file="footer.tpl"}
Comments
Post a Comment