I'm probably over thinking this, but I have site with various user levels. I'd like to create a specific var (either smarty or in PHP) in my site's header template, then based on this value, display certain code on another page. I've tried several approaches without success. I tried creating a simple function in header.php:
if( $user->level_info.level_name == Basic )
{
$basic_user == yes;
}
Then in video.tpl:
{if $basic_user == yes}
some code
{/if}
It didn't work. I also tried it in smarty (header.tpl)
{if $user->level_info.level_name == Basic}
{assign var="basic_user" value="yes"}
{/if}
Then something like if "var=xx, some code", but honestly I can't even remember how I tried that in video.tpl, but I'm sure it was wrong. Can anyone please help? I'm sure it's simple, but I'm stuck and frustrated. Thanks!
I also just tried this in video.php:
if($user->level_info.level_name != Premium){
header("Location: nopermission.php"); exit();
}
And it "works", but all users, no matter if Basic or Premium are forwarded to the no permission page. I am very confused.
For that, I sometimes use session variables with information of user's role.
For example:
$_SESSION['profile'] = 'Basic';
Take a look to session variables. http://www.php.net/manual/en/reserved.variables.session.php
Remember to resume the session in every page in order to access the variables.
session_start();
Using your same code, you can do this:
In header.php:
if($user->level_info.level_name == Basic){
session_start();
$_SESSION['profile'] = 'Basic';
}
Then in video.tpl:
session_start();
if ($_SESSION['profile']=='Basic'){
//some code
}
You can use sessions or cookies
Sessions
page1.php
< ?php
session_start();
// store session data
$_SESSION['user_type']='basic';
?>
page1.php
< ?php
session_start();
if( isset($SESSION['user_type']) ) // checks whether the session variable is already set
{
$user = $SESSION['user_type'];
//here you can compare to all the possible value that the variable may hold
if($user=='Basic')
...
else if($user == 'Admin')
...
}
?>
cookies
page1.php
< ?php
$expire=time()+60*60*24*30;
setcookie("user_type", "Basic", $expire);
?>
page2.php
< ?php
if (isset($_COOKIE["user_type"]))
{
// check here for possible values as shown above
}
?>
You can any of them, but see this another question here on SO
The main difference is security, because if you use cookies directly clients can see and/or edit them themselves, but for session the data is stored on the server side so client cannot access directly.
So if the data only lasts for that session, I prefer using session.
I got it working! Just had to move a version of my original code down a bit in video.tpl:
{if $user->level_info.level_name != Basic}
...
{/if}
Not sure why it works there and not further up, but I tested it, and it does exactly what I need. Thank you VERY much to all who replied!