【发布时间】: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