我想知道像下面这样的东西是否相当安全,并且可以在生产环境中使用。我正在从数据库中检索数据,并使用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>
My PHP file
<?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;
?>
您的问题
将是相当安全的,并且可以在生产环境中使用
你已经覆盖的两个明显区域
- 后端参数化查询
- 被检索的数据不是基于这个站点页面 的用户输入
如果不是这样,那就不用担心了。