我们正在编写一个简单的视频播放器,它的部分功能是向 Web 服务器上的 PHP 页面发送一个简单的 GET 请求,用客户的活动更新数据库(即,他们正在观看的视频的 ID,他们是什么位置,以及他们的用户 ID):
listener.php?user=1&time=59000&movie_id=35003
这个 listener.php 非常简单,它会检查是否已经存在对应这个特定用户 ID 和电影 ID 的行:
SELECT orders_products_id,
products_date_last_watched as lasttime,
now() as now,
products_timewatched
from orders_products
where products_id = '" . $product_id . "' and
customers_id = '" . $customer_id ."'
其次是:
if ($check['orders_products_id'] > 0)
如果为真,它将运行 UPDATE 语句
如果为 false,它将运行 INSERT 语句
现在,问题是,如果我在浏览器中加载这个 listener.php 并直接在 URL 中更改值,它会按预期工作。
但是,当程序调用同一个页面时,它总是会插入一个新行。服务器的日志显示正确的 URL:
"GET /listener.php?user=1&time=128142&movie_id=35003 HTTP/1.1" 200 - "-" "Mozilla/5.0"
有任何想法吗?这是在我的测试服务器上运行的,它是 Windows 2008R2 上的 XAMPP,如果这有什么不同吗?
编辑:这是完整的代码:
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
include('includes/application_top.php');
$user_id = $_GET['user'];
$lastpos = $_GET['time'];
$product_id = $_GET['movie_id'];
$episode = 0;
//check for existing listing
$check_query = tep_db_query("SELECT orders_products_id, products_date_last_watched as lasttime, now() as now, products_timewatched from orders_products where products_id = '" . $product_id . "' and customers_id = '" . $customer_id ."' and products_lastepisode = '" . $episode . "'");
$check = tep_db_fetch_array($check_query);
if ($check['orders_products_id'] > 0) {
//user has already watched this
//find seconds between last timestamp and now
$starttime = strtotime($check['lasttime']);
$endtime = strtotime($check['now']);
$difference = $endtime - $starttime;
if ($difference < 60) {
$totaltime = $check['products_timewatched'] + $difference;
} else {
$totaltime = $check['products_timewatched'];
}
$update_query = "UPDATE orders_products set products_lastposition = '" . $lastpos ."', products_date_last_watched = now(), products_lastepisode = '" . $episode . "', products_timewatched = '" . $totaltime . "', products_platform = '" . $_SERVER['HTTP_USER_AGENT'] . "', customers_ip = '" . $_SERVER['REMOTE_ADDR'] ."' where orders_products_id = '" . $check['orders_products_id'] ."'";
tep_db_query($update_query);
} else {
//create new entry
if ($user_id != 999999999){
tep_db_query("INSERT INTO orders_products (products_date_first_watched, products_visible, products_date_last_watched, customers_id, products_id, products_lastposition, products_lastepisode, products_timewatched) values (now(), 1, now(), '" . $user_id . "', '" . $product_id . "', '" . $lastpos ."', '" . $episode . "', 0)");
}
}
一些注意事项: - 用于 mysql 查询的“tep_db_query”,因为我使用的是 osCommerce 函数的修改版本,它在功能上与标准 mysql_query 和 mysql_fetch_array 相同 - 用户 ID 99999999 表示它是访客用户,不应记录他们的活动 -整个“//查找上一个时间戳和现在之间的秒数”是为了跟踪总时间花费