[英]How to make some content visible only to admin/mod users
I would like to know what the best practice is to restrict access for the user to some parts of the content within my view. 我想知道最好的做法是将用户对我视图内内容的某些部分的访问限制为有限。 I am using CodeIgniter. 我正在使用CodeIgniter。 When the user logs in I set the user data inside the session like this: 当用户登录时,我在会话内设置用户数据,如下所示:
controller 调节器
$this->session->set_userdata('user_id', $user_id);
//..
$this->load->view('view');
Now inside the view there are two DIV
s. 现在,视图内有两个DIV
。 One should be visible to everybody and the other has to only be visible for admin or mods. 一个应该对所有人可见,而另一个仅对管理员或mods可见。 Below is the code. 下面是代码。
view 视图
<div id="log_out">
//visible to everybody
</div>
//Is it possible to put this div as a part of if function? Something like this:
//if <php $this->session->userdata('user_id') ?>
<div id="user_management">
//only visible to admin
</div>
//And how would I enclose the part that is restricted?
Or should this be done completely different? 还是应该完全不同? Should I store somewhere as what the user is currently logged in. If it is regular user, admin or mod? 我应该将用户当前登录的位置存储在某个地方吗?如果是普通用户,管理员或mod? Thank you all in advance for reading and your replies. 预先感谢大家阅读和回复。
I think that better solution is to divide that view to 2 separate views. 我认为更好的解决方案是将该视图分为两个单独的视图。 Then check in controller if user is an admin (read that from session), and load both views if yes, and just one view if is not. 然后在控制器中检查用户是否是管理员(从会话中读取),如果是,则加载两个视图,如果不是,则仅加载一个视图。
Just put a if condition something like this . 只需放置一个if条件,像这样。 1 stands for admin in this case 在这种情况下1代表管理员
<?php if($this->session->userdata('usergroup') == '1') : ?>
---------do something---------
<?php endif;?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.