我已经启动了一个新的应用程序html5,我需要从该应用程序发送信息user_points到数据库我有代码发送固定数量点。 但是现在我需要更改php代码以接收ajax请求更新是数据库中的点来自我的html5应用程序。 我可以改变这个PHP代码来接收ajax变量user_points并存储在数据库中吗?
<?php
session_start();
include_once 'dbconnect.php';
if(!isset($_SESSION['user']))
{
header("Location: index.php");
}
$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
// Retrieve data from database
$result=mysql_query($sql);
//$sql = mysql_query("UPDATE `users` SET points=`points`+5 WHERE user_id='".$_SESSION['user']."'");
$user_id = mysql_real_escape_string($_SESSION['user']);
$sql = mysql_query("UPDATE `users` SET user_points = user_points +'user_points' WHERE user_id = " . $_SESSION['user']);
// close MySQL connection
mysql_close();
?>
&#13;
答案 0 :(得分:0)
假设您的php脚本名为run.php
并假设run.php是通过ajax调用的,如http://yourserver/run.php?user_points=10&userid=12&sessiontan=8162836zqwizß182402834080oio
你的脚本run.php需要看起来像这样:
<?php
session_start();
include_once 'dbconnect.php';
if(isset($_GET['userid']) and isset($_GET['user_points'] and isset($_GET['sessionpin']))) {
$res=mysql_query("SELECT userid FROM sessiontans WHERE user_id=x".dechex($_GET['userid'])" and sessiontan=x".dechex($_GET['sessiontan']));
if($row = mysql_fetch_array($res)) {
$sql = mysql_query("UPDATE `users` SET user_points = user_points + x".dechex($_GET['user_points'])." WHERE user_id = x" . dechex($_GET['userid']));
}
}
// close MySQL connection
mysql_close();
?>
据我所知,会话上下文在名为scripts的ajax中不可用。所以安全方面需要模拟&#39;通过会话或类似的机制。我现在假设,你会遵循sessiontan的方法。一旦用户登录,就会在您的应用程序中创建会话,并且通过时间戳仅在很短的时间范围内(几分钟/小时)有效。会话需要与会话ID不同。
我个人会在用户点上添加更复杂的检查,以确保那些不被欺骗......无论如何,更多的是关于游戏逻辑&#39;并且需要更多地了解这是什么。
同样在性能调优方面,两个语句都可以成为多个查询的一部分:http://php.net/manual/de/mysqli.multi-query.php 再次,这是高级编程策略,超越了你的问题的范围。