我们正在编写一个简单的视频播放器,其部分功能是向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)
如果为true,它将运行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)");
}
}
一些注意事项:-"tep_db_query"用于mysql查询,因为我使用的是osCommerce函数的修改版本,它在功能上与标准的mysql_query和mysql_fetch_array相同-用户id 99999999表示它是访客用户,不应记录他们的活动-整个"//find seconds between last timestamp and now"是为了跟踪花费的总时间