-3

此功能旨在存储在线管理人员。它使用 . 将 IP 地址存储在数据库中ip2long。例如,2130706433。我刚学PHP,不知道怎么传字符串之类的

这是我的代码。

<?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 = ip2long($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


        list($city,$countryName,$countryAbbrev) = explode('|',mysql_real_escape_string(strip_tags($_COOKIE['geoData'])));
    }
    else
    {


        $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];


        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;

?>

这是我的功能代码

 <?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
}
?>

有人可以帮我理解如何使用这个小脚本吗?

4

2 回答 2

1

我不确定你要做什么,你能再解释一下这个问题吗?

从我收集的信息来看,您不想将 IP 存储为数字,而是存储为字符串(IPv4 或 IPv6 格式)。出于以下几个原因,实际上最好以这种格式存储它:

  • 使用更少的存储空间
  • 允许轻松查找以匹配其他 IP 数据库,例如根据 IP 查找原产国
  • 允许创建更有效的索引,整数上的索引总是比字符串上的索引快
于 2013-01-14T16:24:49.573 回答
0
$stringIp = $_SERVER['REMOTE_ADDR'];  // real IP address
$intIp = ip2long($stringIp);  //IP address converted to long int

只需将 $stringIp 而不是 $intIp 插入数据库,您必须确保您的检查正在寻找正确的检查。下面的行需要更改:

$inDB = mysql_query("SELECT 1 FROM tz_who_is_online WHERE ip=".$stringIp);

还有插入查询:

mysql_query("   INSERT INTO tz_who_is_online (ip,city,country,countrycode)
                  VALUES(".$stringIp.",'".$city."','".$countryName."','".$countryAbbrev."')");



mysql_query("UPDATE tz_who_is_online SET dt=NOW() WHERE ip=".$stringIp);

请记住,您的数据库可能需要从当前的列类型更改为 varchar 才能正常工作。否则你会得到插入/更新错误。

于 2013-01-14T16:26:58.247 回答