-1

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.

zoldos
  • 45
  • 1
  • 12

3 Answers3

2

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
    }
Memochipan
  • 3,405
  • 5
  • 35
  • 60
  • It doesn't work. I don't understand. My original conditional if/else statement works elsewhere, but nothing works in video.tpl/video.php. I've tried all methods shown here. – zoldos Dec 14 '13 at 09:25
  • According to PHP documentation $_SESSION "is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script". You have to dig into your code, as it seems as if your problem were somewhere else. What I suppose is that you are not able to access the $user object from there. As you are working with template files, you have to be sure how smarty work with them as they are not processed as php files. Sorry, I've never worked with smarty, so I can't help you further in that topic. – Memochipan Dec 14 '13 at 13:41
  • Looking for it in Google I found that you can pass variables to your templates using specific syntax: http://www.smarty.net/docsv2/en/tips.passing.vars.tpl But maybe that won't be enough for you as you want to show different views to each user. I suggest to work with a view (template) for each user and control which is render from your controllers. Take a look to this: http://www.smarty.net/best_practices – Memochipan Dec 14 '13 at 13:54
0

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.

Community
  • 1
  • 1
Harry
  • 1,572
  • 2
  • 17
  • 31
  • The session method didn't work at all. Very confused :( – zoldos Dec 14 '13 at 09:41
  • @zoldos please be more specific, what didn't work ? is there some error/ exception coming ? try echoing the $SESSION['user_type'] just after setting it and then from that page call your second page and start a new session and then echo it there. If still not working I would recommend to create two empty files and then try it, to keep it simple. – Harry Dec 14 '13 at 09:47
0

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!

zoldos
  • 45
  • 1
  • 12