【问题标题】:Should I use $_GET to dynamically load html?我应该使用 $_GET 来动态加载 html 吗?
【发布时间】:2017-04-01 09:11:32
【问题描述】:

我正在使用 PHP、MySQL(PDO)、JQuery 和 HTML(CSS) 开发一个社交网站。我现在正在处理个人资料页面,我想确定用户是在他们自己的页面上还是在其他页面上,以确定他们将拥有哪些输入。我想为个人资料页面保留一个框架并加载相应的选项,而不是拥有 2 个不同的页面(我的个人资料与其他个人资料)。

我正在学习 JQuery 和 PHP,所以现在我在想,在 JQuery 中,我可以获取 $_GET (?username= ) 并将其与登录的用户 ID 进行比较,然后加载基于此我需要的组件。

或者我在 PHP 中确定用户,然后运行某个脚本来加载组件。

这些方法中的哪一种会起作用,还是有不同/更好的方法来做到这一点?

【问题讨论】:

    标签: php jquery html css mysql


    【解决方案1】:

    我认为你不能在 JavaScript 中使用 $_GET

    取而代之的是URLSearchParams

    const url = new URL('http://domain/path?username=someone');
    const username = new URLSearchParams(url.search).get('username)'; // someone
    

    【讨论】:

      【解决方案2】:

      最佳安全实践是不要依赖浏览器中发生的任何事情。

      这意味着您应该始终弄清楚服务器上的权限(在 PHP 端)。

      变体 1(简单)

      假设您已经对用户进行了身份验证,并且在 PHP 中用户为 $currentUser,其 ID 为 $currentUser->id

      现在这个用户想通过请求 url "profile?id=555" 来查看一些个人资料

      这就是(基本上)你在 PHP 方面所做的事情:

      $requestedProfile = (int)$_GET['profile'];
      if ($requestedProfile == $currentUser->id) {
          // they want own profile
          show_controls_for_own_profile();
      } else {
          // they want someone else's profile
          show_controls_for_other_profile();
      }
      

      在这种情况下,jQuery 与这里无关。

      变体 2(单独的请求)

      此外,假设您要缓存整个个人资料页面以将其快速加载到浏览器中,然后才通过 ajax-load 加载与用户权限相关的其他控件。

      那么你有两个控制器方法(php 脚本,等等),每个都服务于它自己的部分:

      public function showProfile() {
          $requestedProfile = (int)$_GET['profile'];
          if (profile_exists($requestedProfile)) {
              show_the_profile($requestedProfile); // no check here
          }
      }
      

      上述方法将通过其 ID(带有空的“.controls”元素)返回通用配置文件页面,并且在该页面上,一些 jquery 代码会要求服务器返回适当的用户变体 -依赖部分。

      $.ajax('/user-controls.php')
          .done(function(response) { $('.controls').html(response); });
      

      请注意,它不会告诉服务器任何用户 ID - 服务器应该已经知道会话中当前经过身份验证的用户 ID!

      第二个函数,和开头一样,将只返回控件的 HTML:

      // Example of getting current profile from server's headers
      function get_viewed_profile_id()
      {
          // url of previous profile page, e.g. http://example.com/profile?id=555
          $referer = $_SERVER['HTTP_REFERER']; 
          $query = parse_url($referer, PHP_URL_QUERY); // id=555
          parse_str($query, $params); // ['id' => '555']
          return $params['id']; // 555
      }
      
      // we should store the authenticated user in session, 
      // not rely on anything user sends from their browser
      $currentUserId = $_SESSION['user']->id; 
      $requestedProfileId = get_viewed_profile_id();
      
      // now that we know both viewed profile and current user,
      // we can compare them
      if ($requestedProfileId == $currentUserId) {
          // they want own profile
          show_controls_for_own_profile();
      } else {
          // they want someone else's profile
          show_controls_for_other_profile();
      }
      

      变体 3(将 PHP 变量传递给 javascript)

      同上,但你不依赖$_SERVER['HTTP_REFERER'],而是将一些逻辑转移到Javascript:

      浏览器询问:

      // look up currently browsed profile from current window url
      var q = new URLSearchParams(window.location.search);
      var controlsURL = '/user-controls.php?viewedProfile=' + q.get('id');
      // ask for controls, specifying the currently browsed profile
      $.ajax(controlsURL)
          .done(function(response) { $('.controls').html(response); });
      

      服务器响应:

      function get_viewed_profile_id()
      {
          return (int)$_GET['viewedProfile'];
      }
      
      // we should store the authenticated user in session, 
      // not rely on anyhting user sends from their browser
      $currentUserId = $_SESSION['user']->id; 
      $requestedProfileId = get_viewed_profile_id();
      
      // now that we know both viewed profile and current user,
      // we can compare them
      if ($requestedProfileId == $currentUserId) {
          // they want own profile
          show_controls_for_own_profile();
      } else {
          // they want someone else's profile
          show_controls_for_other_profile();
      }
      

      【讨论】:

      • 那么最好的做法是将“动态”部分(加载相应的控件)留在 php 文件中的 html 中或使其调用脚本?
      • 如果您的服务器需要高负载,那么我希望尽可能多地缓存配置文件页面,这意味着从中删除所有动态元素,在这种情况下,是的。
      • 让它完全按照我想要的方式工作,并进行了一些修改。谢谢!
      【解决方案3】:

      在 jquery 中你可以使用带有数据类型 html 的 $.get()

      【讨论】:

        【解决方案4】:

        您可以在 JavaScript 中回显 PHP 的 GET 值。但请确保,如果没有 makeItSafe(您必须自己定义),则可以利用此代码执行跨站点脚本攻击:

        <script>
        var myGlobalUserID = "<?php echo makeItSafe($_GET['userID']); ?>";
        </script>
        

        【讨论】:

          【解决方案5】:

          只需在页面上声明一个隐藏字段

          <input type="hidden" id="username" value="<?php echo $_GET['username'];?>">
          

          在其中获取用户名值。 然后只需在 jquery 中获取该值

          <script>
          $(document).ready(function(){
          var username = $("#username").val();
          if(username == "abc"){
          $("#div_one").show();
          $("#div_two").hide();
          }
          });
          </script>
          

          使用 if() 条件和 hide() , show() 方法加载您想要显示给用户的组件。

          【讨论】:

            猜你喜欢
            • 2014-07-08
            • 1970-01-01
            • 2011-04-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-08
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多