简体   繁体   English

如何根据用户是否登录来显示内容

[英]How can I display content depending on if user is logged in or not

I am trying to make a web app that lets users share information, but my partner wants a specific type of logic.我正在尝试制作一个允许用户共享信息的网络应用程序,但我的合作伙伴想要一种特定类型的逻辑。 If the user is logged in, he or she will get more content than if he or she wasn't.如果用户已登录,他或她将获得比未登录时更多的内容。 To be specific, I want the person who ISN'T logged in to be able to see projects, grids, tiles, and sources.具体来说,我希望未登录的人能够查看项目、网格、图块和源。 Then, I want the person who IS logged in to be able to not only see everything above, but gain options like creating, editing and deleting projects, as well as give the user the opportunity to brainstorm ideas via a link.然后,我希望登录的人不仅可以看到上面的所有内容,还可以选择创建、编辑和删除项目,并让用户有机会通过链接集思广益。

<!-- Tab Links -->
      <div class="tab">
        <button class="tablinks" onclick="openTab(event, 'Dashboard')">Dashboard</button>
        <button class="tablinks" onclick="openTab(event, 'Projects')">Projects</button>
        <button class="tablinks" onclick="openTab(event, 'Grids')">Grids</button>
      </div>

      <!-- Tab Contents -->
      <div id="Dashboard" class="tabcontent">
        <h3>Dashboard</h3>
        <p>Projects, Grids, Tiles, and Sources go here.</p>
      </div>

      <div id="Projects" class="tabcontent">
        <h3>Projects</h3>
        <p>Project titles, grids, tiles, and sources go here.</p>
      </div>

      <div id="Grids" class="tabcontent">
        <h3>Grids</h3>
        <p>Grid titles, tabs, tiles, and sources go here.</p>
      </div>

That is the HTML, and here is the Javascript:那是 HTML,这里是 Javascript:

//When you click a dashboard tabs
    openTab: async function(evt, tabName) {
      // Declare all variables
      var i, tabcontent, tablinks;

      // Get all elements with class="tabcontent" and hide them
      tabcontent = document.getElementsByClassName("tabcontent");
      for (i = 0; i < tabcontent.length; i++) {
        tabcontent[i].style.display = "none";
      }

      // Get all elements with class="tablinks" and remove the class "active"
      tablinks = document.getElementsByClassName("tablinks");
      for (i = 0; i < tablinks.length; i++) {
        tablinks[i].className = tablinks[i].className.replace("active", "");
      }

      // Show the current tab, and add an "active" class to the button that opened the tab
      document.getElementById(cityName).style.display = "block";
      evt.currentTarget.className += " active";
    }

I would appreciate any help I can get.我很感激我能得到的任何帮助。

As others have stated, it's best not to include "authenticated" DOM elements to unauthenticated users.正如其他人所说,最好不要向未经身份验证的用户包含“经过身份验证的”DOM 元素。 That said, a nice pattern is to set an "authenticated" class on the html element (if the server determines that the user is authenticated).也就是说,一个不错的模式是在 html 元素上设置一个“经过身份验证的”类(如果服务器确定用户已通过身份验证)。 Then use descendent css to your heart's desire.然后根据您的愿望使用后代css。 Right now you're using javascript for formatting that is best handled by css.现在您正在使用 javascript 进行格式设置,最好由 css 处理。

You can create a session variable ( in PHP for example : https://www.php.net/manual/en/book.session.php ) and display build your content accordingly.您可以创建一个会话变量(例如在 PHP 中: https : //www.php.net/manual/en/book.session.php )并相应地显示构建您的内容。 I speak about PHP because security ( and so authentification ) is a server-side job.我谈论 PHP 是因为安全(以及身份验证)是服务器端的工作。

You need to AJAX the "actions" HTML from the server.您需要对来自服务器的“动作”HTML 进行 AJAX。 This is a two part deal.这是一个两部分的交易。

Client side:客户端:

// execute this on load
$.ajax({
     type: "GET",
     url: '/getactions',
     success: function(data) {
           // data is ur summary
          $('#buttons_div').html(data);
     }
});

Server Side: (assuming you are using php, getactions.php)服务器端:(假设你使用的是 php,getactions.php)

<?php
if( !isset($_SESSION['user_is_logged']) || (time() - $_SESSION['last_access']) > 60 )
  $_SESSION['last_access'] = time();
  echo '<button>Create New Project</button>'
?>

Something like that...类似的东西...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM