我們正在編寫一個簡單的視頻播放器,它的部分功能是發送一個簡單的GET請求到Web服務器上的PHP頁面,以便用客戶的活動(即,他們正在觀看的視頻的ID,他們是什麼樣的地位,他們的用戶ID):當頁面沒有從瀏覽器請求頁面時,PHP的行爲不同
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語句
如有虛假,它會運行一個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查詢,因爲我使用的自從職能的改進版本,它的功能也可以通過標準的mysql_query和mysql_fetch_array - 用戶ID 99999999意味着它是一個guest用戶和他們的活動不應該被記錄 - 整個「//找到最後的時間戳和現在之間秒」是跟蹤的總時間花費
你能發佈完整的腳本嗎? – j08691
將查詢作爲調試回顯到錯誤日誌中。我敢打賭你沒有得到正確的參數? – Nanne
由於他正在使用MySQL函數中的NOW()而不是他所擁有的日期字段,所以時間總是不一樣。 – Prix