在线上谁在线 [英] live who is online on website

查看:57
本文介绍了在线上谁在线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小脚本,谁能告诉我谁在线或网站,但在db中是由ip2long存储的,我想存储用户的真实IP地址....请有人可以帮助我吗?这是我的代码`

i have a small script who show me who is online o website but in db is stored by ip2long and i want to stored the real ip adress of user ....please can sombody can help me with this? here is my codes `

<?php

require "connect.php";
require "functions.php";

// We don't want web bots scewing our stats:
if(is_bot()) die();



$stringIp = $_SERVER['REMOTE_ADDR'];
$intIp = long2ip($stringIp);



// Checking wheter the visitor is already marked as being online:
$inDB = mysql_query("SELECT 1 FROM tz_who_is_online WHERE ip=".$intIp);

if(!mysql_num_rows($inDB))
{
    // This user is not in the database, so we must fetch
    // the geoip data and insert it into the online table:

    if($_COOKIE['geoData'])
    {
        // A "geoData" cookie has been previously set by the script, so we will use it

        // Always escape any user input, including cookies:
        list($city,$countryName,$countryAbbrev) = explode('|',mysql_real_escape_string(strip_tags($_COOKIE['geoData'])));
    }
    else
    {
        // Making an API call to Hostip:

        $xml = file_get_contents('http://api.hostip.info/?ip='.$stringIp);

        $city = get_tag('gml:name',$xml);
        $city = $city[1];

        $countryName = get_tag('countryName',$xml);
        $countryName = $countryName[0];

        $countryAbbrev = get_tag('countryAbbrev',$xml);
        $countryAbbrev = $countryAbbrev[0];

        // Setting a cookie with the data, which is set to expire in a month:
        setcookie('geoData',$city.'|'.$countryName.'|'.$countryAbbrev, time()+60*60*24*30,'/');
    }

    $countryName = str_replace('(Unknown Country?)','UNKNOWN',$countryName);

    // In case the Hostip API fails:

    if (!$countryName)
    {
        $countryName='UNKNOWN';
        $countryAbbrev='XX';
        $city='(Unknown City?)';
    }

    mysql_query("   INSERT INTO tz_who_is_online (ip,city,country,countrycode)
                    VALUES(".$intIp.",'".$city."','".$countryName."','".$countryAbbrev."')");
}
else
{
    // If the visitor is already online, just update the dt value of the row:
    mysql_query("UPDATE tz_who_is_online SET dt=NOW() WHERE ip=".$intIp);
}

// Removing entries not updated in the last 10 minutes:
mysql_query("DELETE FROM tz_who_is_online WHERE dt<SUBTIME(NOW(),'0 0:10:0')");

// Counting all the online visitors:
list($totalOnline) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM tz_who_is_online"));

// Outputting the number as plain text:
echo $totalOnline;

?>




and this is for functions:


<?php


function get_tag($tag,$xml)
{
    preg_match_all('/<'.$tag.'>(.*)<\/'.$tag.'>$/imU',$xml,$match);
    return $match[1];
}

function is_bot()
{
    /* This function will check whether the visitor is a search engine robot */

    $botlist = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi",
    "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
    "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
    "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
    "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
    "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
    "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
    "Butterfly","Twitturls","Me.dium","Twiceler");

    foreach($botlist as $bot)
    {
        if(strpos($_SERVER['HTTP_USER_AGENT'],$bot)!==false)
        return true;    // Is a bot
    }

    return false;   // Not a bot
}
?>

`

推荐答案

应该将IP地址 存储为 ip2long 函数的结果.然后,当您从数据库中获取结果时,可以使用 long2ip 函数将整数转换为点符号IP地址.

IP addresses should be stored as the result of the ip2long function. You can then use the long2ip function to convert the integer to a dot-notation IP address when you fetch the result from the database.

准备要从数据库中插入IP地址:

Preparing an IP address to be inserted from the database:

$ip = ip2long($_SERVER['REMOTE_ADDR']);

从数据库中获取数据库,并以点符号格式返回它:

Taking a database from the database and returning it in the dotted-notation format:

$ip = long2ip($row['ip_address']);

简单.

这篇关于在线上谁在线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆