【问题标题】:Fairly Secure for live site?现场站点相当安全?
【发布时间】:2015-10-09 17:06:24
【问题描述】:

我想知道以下内容是否相当安全并且可以在生产环境中使用。我正在从数据库中检索数据并使用 Chart.js 将响应数据用于图表。

我的 html 文件

<div id="canvas-holder">
    <canvas id="chart-area2" width="200" height="200" />
</div>

<div id="canvas-holder">
    <canvas id="chart-area" width="200" height="200" />
</div>

<div id="chartjs-tooltip"></div>


<script>
$.ajax({
   url: 'chartpi.php',
   success: function (response) {//response is value returned from php 
        var datachart = JSON.parse(response);
        var ctx2 = document.getElementById("chart-area2").getContext("2d");
        window.myPie = new Chart(ctx2).Pie(datachart);
   }
});

$.ajax({
   url: 'chartpi2.php',
   success: function (response) {//response is value returned from php
        var datachart = JSON.parse(response);
        var ctx = document.getElementById("chart-area").getContext("2d");
        window.myPie = new Chart(ctx).Doughnut(datachart);
   }
});
</script>

我的 PHP 文件

<?php

        // set up the connection variables
        $db_name  = '$dbname';
        $hostname = '$host';
        $username = '$uname';
        $password = '$pass';

        // connect to the database
        $dbh = new PDO("mysql:host=$hostname;dbname=$db_name", $username, $password);

        // a query get all the records from the users table
        $sql = 'SELECT * FROM pichart2';

        // use prepared statements, even if not strictly required is good practice
        $stmt = $dbh->prepare( $sql );

        // execute the query
        $stmt->execute();

        // fetch the results into an array
        $result = $stmt->fetchAll( PDO::FETCH_ASSOC );

        // convert to json
        $json = json_encode( $result );

        // echo the json string
        echo $json;
?>

【问题讨论】:

  • 乍一看,这看起来相当安全。
  • 好的,谢谢。我对 php 安全性还很陌生,所以对于可以改进代码以更安全的地方我有点困惑。
  • 确保您对请求使用 SSL
  • 你主要关心的是用户输入,但这没有
  • @Ben 不需要用户输入

标签: php jquery json ajax security


【解决方案1】:

你的问题

相当安全,可以在生产环境中使用

你已经覆盖了两个明显的区域

  • 后端的参数化查询
  • 检索的数据不是基于来自网站页面的用户输入

但是,我要提醒的是,如果从饼状表中检索到的任何数据保留了来自其他来源的任何用户提供的数据,那么您应该考虑/实施正确的 output encoding 甚至 如果执行了适当的输入卫生。

如果不是这样,那就不用担心了。

【讨论】:

  • 感谢您的回复。当我决定将用户输入添加到图表中时,我会更多地研究编码以供将来使用。
猜你喜欢
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 2011-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多