dtrj74376 2014-05-26 14:34
浏览 40
已采纳

将用户会话保存为变量

Pretty new to php so bear with me.

I am using a simple template system where i set a variable as [%PLACEHOLDER%] that then outputs the data stored in the variable on the template page when ever i use the [%TAG%] associated with it.

I do this as i like to separate all of the php and html making it easier for different themes and templates to be created with out a user knowing php.

a simple example is :

(object) $tpl = new Tmpl( array( 'file' => 'templates/header.tpl' ) );

$tpl->replace( '[%USER%]', $user);
echo $tpl->output();

Now this all works fine. what i want to do is save a user session as a variable for me to assign a placeholder tag to it.

I use the following code with a php page

<div id="logedin" class="<?php echo (isset($_SESSION['sess_user_id'])) ? 'show' : 'hide'; ?>">
Welcome, <?php echo $_SESSION["sess_username"] ?>

basically if the user is logged in the div id 'logedin' is shown. if not its hidden.

there are 2 css styles i use for this.

.show{
 display: block;
}
.hide{
 display: none;
}

what i want to do is save the (isset($_SESSION['sess_user_id'])) ? 'show' : 'hide'; as a variable i can then assign as a placeholder.

something like this (but this obviously does not work hence me asking the question)

$sessionclassshow = (isset($_SESSION['sess_user_id'])) ? 'show' : 'hide';
$sessionclasshide = (isset($_SESSION['sess_user_id'])) ? 'hide' : 'show';

then i assign them as a place holder like this

$tpl->replace( '[%CLASSSHOW%]', $sessionclassshow);
$tpl->replace( '[%CLASSHIDE%]', $sessionclasshide);

so anywhere in my template file i can show or hide a div dependent on if the user is loged in or not.

<div id="topmenu" class="[%SESSIONHIDE%]"><a href="login.html">Login</a></div>
<div id="welcome" class="[%SESSIONSHOW%]">welcome [%USER%]</div>

so if the user is logged in hide the 'topmenu' div and show the 'welcome' div and vice versa.

I hope i have explained everything well enough and appreciate any help

thanks lee

  • 写回答

1条回答 默认 最新

  • du0173 2014-05-26 14:42
    关注

    You assign them like this :

    $tpl->replace( '[%CLASSSHOW%]', $sessionclassshow);
    $tpl->replace( '[%CLASSHIDE%]', $sessionclasshide);
    

    But in the template you use them using %SESSIONXXXX% when you should use %CLASSXXXX%(as in their declaration).

    This should work :

    <div id="topmenu" class="[%CLASSHIDE%]"><a href="login.html">Login</a></div>
    <div id="welcome" class="[%CLASSSHOW%]">welcome [%USER%]</div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)