[英]Working with online users based on their IP address address - mysql database
現在,我有一個框(在頁腳中),其中根據三種類型的URL位置(用戶類型和其IP地址)向在線用戶顯示。
我正在使用單個查詢來刪除,插入和顯示在線用戶,但是我確實需要更高的性能,因為我不確定這是否是最快的方法。 也許有人在我的代碼中看到任何不良做法。
開始了:
網址位置的類型:
用戶類型:
這是我獲取用戶IP地址的方式:
$ip_lookup = array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR');
foreach($ip_lookup as $server_param)
{
if(isset($_SERVER[$server_param]) && filter_var($_SERVER[$server_param], FILTER_VALIDATE_IP))
{
if(filter_var($_SERVER[$server_param], FILTER_VALIDATE_IP, FILTER_FLAG_IPV6))
{
define('dpd_ip_type','v6');
}
else
{
define('dpd_ip_type','v4');
}
define('dpd_ip',inet_pton($_SERVER[$server_param]));
break;
}
}
if(!defined('dpd_ip') || dpd_ip === false) { error('error_ip'); exit(); }
在這里,我有一個數組(其中一個基於url類型或用戶類型):
$online['dpd_ip'] = dpd_ip; /* The ip (ipv4 or ipv6) - this was converted as binary width inet_pton */
$online['dpd_ip_type'] = dpd_ip_type; /* The ip type (ipv4 or ipv6) */
$online['dpd_time'] = dpd_time; /* The current time in Unix Timestamp format */
$online['url'] = ''; /* The url location - Empty by default */
$online['user_id'] = ''; /* The user id - Empty by default */
$online['user_name'] = ''; /* The user name - Empty by default */
$online['get_online_users_where'] = ''; /* The where clause used for for output records - empty by default */
$online['online_clear_where'] = ''; /* The where clause used for deleting records - empty by default */
if(is_user())
{
$online['user_id'] = user_id; /* Set user_id because we talk about an registered user */
$online['user_name'] = user_name; /* Set user_name because we talk about an registered user */
$online['online_clear_where'] = " OR online_user_id = ".user_id; /* Set where clause for deleting this user before insert him again */
}
if(defined('url_1') && defined('url_2') && (url_1 == 'topic' || url_1 == 'forum'))
{
$online['url'] = url_1.'/'.url_2; /* The url is a section like forum/123/ or topic/123 */
$online['get_online_users_where'] = " WHERE online_url = '".url_1.'/'.url_2."'"; /* Output the user that are accessed this url location */
}
在這里,我開始使用數據庫:
pdo('begin');
/* Delete records from database if:
- The records are bigger than 15 minutes ( dpd_footer_online_time is set to 900 seconds )
- This is the actual user
*/
$online_clear = pdo("DELETE FROM online WHERE online_timestamp < ".(dpd_time - dpd_footer_online_time)." OR online_ip_".$online['dpd_ip_type']." = '".$online['dpd_ip']."'" . $online['online_clear_where']);
/* Insert the user into database */
$online = pdo("
INSERT INTO
online
(
online_user_id,
online_user_name,
online_ip_".$online['dpd_ip_type'].",
online_timestamp,
online_url
)
VALUES
(
'".$online['user_id']."',
'".$online['user_name']."',
'".$online['dpd_ip']."',
'".$online['dpd_time']."',
'".$online['url']."'
)
"
);
/* Output the users from the last 15 minutes */
$get_online_users = pdo("SELECT online_user_id, online_user_name, online_timestamp FROM online".$online['get_online_users_where']);
pdo('commit');
當然,這是表(online_ip_v4和online_ip_v6是唯一的):
任何建議都歡迎...
請SHOW CREATE TABLE online;
提供SHOW CREATE TABLE online;
這樣我們就可以看到您擁有什么索引(如果有),等等。
使用賦值語句,而不是define(...)
。
使用綁定而不是串聯-您容易受到“ SQL注入”的攻擊。
將DELETE拆分為兩個(或三個)DELETE-對應於OR的每一側。 (或優化效果不佳。)
你可能需要
INDEX(online_timestamp)
UNIQUE(online_ip_v4)
UNIQUE(online_ip_v6)
INDEX(user_id)
對於沒有IPv4和IPv6的網站,您該怎么辦?
請提供“顯示在線用戶”的查詢/查詢。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.