【问题标题】:Making menu items visible/invisible according to user role根据用户角色使菜单项可见/不可见
【发布时间】:2018-04-17 14:29:17
【问题描述】:

我使用 PHP 脚本(AdLinkFly - 货币化网址缩短器)

我想根据登录用户显示不同的菜单。如果用户是客户,则显示管理帐户和注销,否则显示 Sing-Up 和 Sing-In。

这里是主页http://adshort.ga 这是代码“front.ctp”:

<html lang="<?= locale_get_primary_language(null) ?>">
<head>
    <?= $this->element('front_head'); ?>
</head>
<body class="<?= ($this->request->here == $this->request->webroot) ? 'home' : 'inner-page' ?>">
<?= get_option('after_body_tag_code'); ?>

<!-- Navigation -->
<nav id="mainNav" class="navbar navbar-default navbar-fixed-top">
    <div class="container">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only"><?= __('Toggle navigation') ?></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>

            <?php
            $logo = get_logo();
            $class = '';
            if ($logo['type'] == 'image') {
                $class = 'logo-image';
            }
            ?>
            <a class="navbar-brand <?= $class ?>" href="<?= $this->Url->build('/'); ?>"><?= $logo['content'] ?></a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                
                <li>
                    <a href="<?= $this->Url->build('/'); ?>"><?= __('Home') ?></a>
                </li>
                
                
                <?php if (get_option('enable_advertising', 'yes') == 'yes') : ?>
                    <li>
                        <a href="<?= $this->Url->build('/advertising-rates'); ?>"><?= __('Advertising') ?></a>
                    </li>
                    
                    
                <?php endif; ?>
                
                
                <li>
                    <a href="<?= $this->Url->build('/payout-rates'); ?>"><?= __('Payout Rates') ?></a>
                </li>
                
                
                <?php if ((bool)get_option('blog_enable', false)) : ?>
                    <li>
                        <a href="<?= $this->Url->build('/blog'); ?>"><?= __('Blog') ?></a>
                    </li>
                <?php endif; ?>
                
                <li>
                    <a href="<?= $this->Url->build('/auth/signin'); ?>"><?= __('Sign in') ?></a>
                </li>
                <li>
                    <a href="<?= $this->Url->build('/auth/signUP'); ?>"><?= __('Sign Up') ?></a>
                </li>
                <li>
                    <a href="<?= $this->Url->build('/member/dashboard'); ?>"><?= __('Dashboard') ?></a>
                </li>
               
            
                <?php if (count(get_site_languages(true)) > 1) : ?>
                    <li class="dropdown language-selector">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                           aria-expanded="false"><i class="fa fa-language"></i> <span class="caret"></span></a>
                        <ul class="dropdown-menu">
                            <?php foreach (get_site_languages(true) as $lang) : ?>
                                <li>
                                    <?= $this->Html->link(
                                        locale_get_display_name($lang, $lang),
                                        '/' . $this->request->url . '?lang=' . $lang
                                    ); ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </li>
                <?php endif; ?>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container-fluid -->
</nav>

<?= $this->Flash->render() ?>
<?= $this->fetch('content') ?>

<?= $this->element('front_footer'); ?>

</body>

</html>

我不是 PHP 专家,如果有人可以帮助我,我将不胜感激

【问题讨论】:

标签: javascript php cakephp


【解决方案1】:

当用户登录时,启动一个sessionsession_start(),然后在存储一个脚本可以识别的值时添加到$_SESSION变量,例如:$_SESSION['userLogged'] = true;,然后在主文件中,你可以添加这个:

      session_start();
      if($_SESSION['userLogged'] == true) {
         //Display here the HTML with echo function
      } else {
         //Load the normal page
      }

假设您想显示一个带有文本“Hello world!”的&lt;p&gt; 标签,那么您必须转到 html 文件,找到要执行代码的标签,然后执行代码以下。例如:

    if($_SESSION['userLogged'] == true) {
       echo '<p>Hello world!</p>'; 
    } 

【讨论】:

  • 您好,感谢您的快速重播。我不是这方面的专家,如果可能的话,您能帮我解释一下吗?
  • 我已经编辑了答案,如果你现在明白了,请告诉我@AchrafYo
猜你喜欢
  • 1970-01-01
  • 2021-10-22
  • 1970-01-01
  • 2014-03-20
  • 2011-11-17
  • 2011-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多