【发布时间】:2016-12-23 05:44:08
【问题描述】:
我正在使用 ActionScript 3.0 在 Flash 中创建游戏。我将数据传递给 PHP 以检查、插入游戏结果并向 API URL 发送请求。
但是,我遇到了一些失败的插入查询,一些失败的 cURL,有时两者兼而有之?为什么会这样?
我正在使用 RDS 数据库(t2.Medium)。
这是我在 ActionScript 中的代码:
var variables:URLVariables = new URLVariables();
var varSend:URLRequest = new URLRequest(link + "parse.php");
varSend.method = URLRequestMethod.POST;
varSend.data = variables;
// Build the varLoader variable
var varLoader:URLLoader = new URLLoader;
varLoader.dataFormat = URLLoaderDataFormat.BINARY;
//varLoader.addEventListener(Event.COMPLETE, completeHandler);
variables.apikey = "<API KEY>";
variables.username = <FETCH FROM JS>
variables.side = "Good";
variables.player1 = player1;
variables.player2 = player2;
variables.player3 = player3;
variables.player4 = player4;
variables.player5 = player5;
variables.jackpot1 = jackpot1;
variables.jackpot2 = jackpot2;
variables.jackpot3 = jackpot3;
variables.jackpot4 = jackpot4;
variables.jackpot5 = jackpot5;
variables.sendRequest = "parse";
// Send the data to the php file
varLoader.load(varSend);
这是我的 PHP 代码:
<?php
if ($_POST['sendRequest'] == "parse") {
$datetime = date('Y-m-d H:i:s');
$datetime1 = date('Y-m-d');
$apikey = $_POST['apikey'];
$promocode = "TestGame";
$username = $_POST['username'];
$alignment = $_POST['side'];
$player1 = $_POST['player1'];
$player2 = $_POST['player2'];
$player3 = $_POST['player3'];
$player4 = $_POST['player4'];
$player5 = $_POST['player5'];
$player = $player1 + $player2 + $player3 + $player4 + $player5;
$jackpot1 = $_POST['jackpot1'];
$jackpot2 = $_POST['jackpot2'];
$jackpot3 = $_POST['jackpot3'];
$jackpot4 = $_POST['jackpot4'];
$jackpot5 = $_POST['jackpot5'];
$jackpot = $jackpot1 + $jackpot2 + $jackpot3 + $jackpot4 + $jackpot5;
$db_servername = "<RDS Host>";
$db_username = "<Database User>";
$db_password = "<DB Password>";
$db_name = "<DB Name>";
$connection = mysqli_connect($db_servername, $db_username, $db_password, $db_name);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$check_sql = "SELECT * FROM tblpoints WHERE username = '$username' AND date(datetime) = '$datetime1'";
$result = mysqli_query($connection,$check_sql);
if (!$result) {
echo "Error in checking record: " + mysqli_error($connection);
exit;
}
$points = $player;
$betcondition = $points * 10;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($ch,CURLOPT_URL, "<API URL>".$points."/".$betcondition."/".$username);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
$result = curl_exec($ch);
if(curl_errno($ch)) {
print "Error: " . curl_error($ch);
} else {
curl_close($ch);
}
$sql = "INSERT INTO tblpoints (username, alignment, player_points, player_points1, player_points2, player_points3, player_points4, player_points5, jackpot_points, jackpot_points1, jackpot_points2, jackpot_points3, jackpot_points4, jackpot_points5, datetime, status) VALUES ('$username', '$alignment', '$player', '$player1', '$player2', '$player3', '$player4', '$player5', '$jackpot', '$jackpot1', '$jackpot2', '$jackpot3', '$jackpot4', '$jackpot5', '$datetime', '$status')";
mysqli_query($connection,$sql);
mysqli_close($connection);
}
?>
注意:这里不会有用户直接输入。
【问题讨论】:
-
这里面有一些严重的 SQL 注入漏洞。
-
@halfer 嗨,你是什么意思?我只是 php 和 actionscript 的新手,只是想提高我的技能:)
-
这意味着您有安全问题,read this 了解更多。至于数据库查询失败的原因,你必须记录
$check_sql,当它无法找出它失败的原因时。当然,如果datetime不是有效日期,或者$username包含撇号,它将失败并出现数据库错误。 -
当你得到一个数据库错误时,它会说什么?
-
警告:当使用
mysqli时,您应该使用parameterized queries 和bind_param将用户数据添加到您的查询中。 不要使用字符串插值或连接来完成此操作,因为您创建了一个严重的SQL injection bug。 切勿将$_POST或$_GET数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。
标签: php mysql actionscript-3 flash curl