Hide and show a div depending on session

瘦欲@ 提交于 2019-12-13 05:28:56

问题


I have an admin link with a div id "admin". Sessions are started when a user is logged in to show if it is a normal user or an admin. Normal users can't access the files for admin, but can still see the admin link.

Is there a way to make it so normal users can't see the link, using only php or html, without jquery or jscript or any of those.


回答1:


Using interleaved PHP & HTML with standard PHP syntax:

<?php
if ($user_is_an_admin) {
?>
<div id='admin'>
  Only admins can see this...
</div>
<?php
}
?>

Alternate templating syntax:

<?php if ($user_is_an_admin): ?>
<div id='admin'>
      Only admins can see this...
</div>
<?php endif; ?>

Not interleaving, PHP only:

if ($user_is_an_admin) {
  echo "<div id='admin'>
      Only admins can see this...
     </div>
  ";
}



回答2:


You'll need to use conditionals inside of your views:

<?php if($_SESSION['adminid'] == 1234): ?>
    <!-- Admin div goes here -->
<?php else: ?>
    <!-- Admin link goes here -->
<?php endif; ?>


来源:https://stackoverflow.com/questions/8964856/hide-and-show-a-div-depending-on-session

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!