【问题标题】:PHP: managing url $_GET tinkeringPHP:管理 url $_GET 修补
【发布时间】:2010-10-25 14:24:40
【问题描述】:

在这种情况下,我有一个支持工单列表,当您单击工单的标题时,您会进入一个更详细地显示工单的页面。如果使用 URL GET 变量来查询数据库。我已经考虑了 SQL 注入,但是如果有人将 url 修改为不存在的 id 怎么办?解决这个问题的最佳方法是什么?

谢谢,

琼斯

【问题讨论】:

    标签: php url exception-handling get


    【解决方案1】:

    如果 ID 不存在,发送一个 404 - Not Found 标头以及一个漂亮的错误页面,告诉用户它没有找到。

    【讨论】:

      【解决方案2】:

      无论如何,您可能不得不创建一个页面来处理不成功的搜索;把它路由到那里。然后,您可以帮助用户以一致的方式找到他搜索的内容,提供线索和“最常搜索”以及其他内容。

      【讨论】:

        【解决方案3】:

        这似乎太简单了,但您应该始终在对 GET(或 POST)变量进行任何操作之前对其进行验证。在您的情况下,只需验证该 ID 是否存在于数据库中。如果没有,请通知用户。

        【讨论】:

          【解决方案4】:

          您应该始终检查您的查询是否返回任何内容。如果返回0行,则ID不存在。

          <?php
          
          $result = mysql_db_query("your query", $link);
          $num_rows = mysql_num_rows($result);
          
          if($num_rows < 1) {
            // row with that id doesnt exist
            // do whatever you want
          } elseif($num_rows > 1) {
            // you have problem with your ids in db
          } else {
            // everything went fine
            // do your thing here
          }
          
          ?>
          

          【讨论】:

            【解决方案5】:

            检查票证是否存在;如果没有,请做出相应的反应。什么“相应地做出反应”是由您的业务逻辑决定的:创建一个新工单?引发错误?将用户带到可用票证列表?

            为简洁起见,使用旧的 mysql 扩展的示例:

            $sanitized_numeric_id = (int) $_GET['ticket_id']; // assuming id is numeric
            $query_resource = mysql_query('SELECT `somecolumn`, `column2`, `othercolumn` 
                                              FROM `tickets`
                                              WHERE `id`= ' . $sanitized_numeric_id);
            if (mysql_num_rows($query_resource) > 0) {
                // the ticket exists, do something with it
            } else {
                // the ticket doesn't exist, react accordingly
            }
            

            【讨论】:

              猜你喜欢
              • 2019-03-12
              • 2023-04-09
              • 2012-05-10
              • 2010-10-05
              • 2011-07-07
              • 1970-01-01
              • 1970-01-01
              • 2012-03-27
              • 1970-01-01
              相关资源
              最近更新 更多