使用PHP和mySQLi的手工计数器脚本的问题

时间:2019-07-14 14:29:20

标签: php mysql mysqli

我有一个“手工”迷你计数器脚本,该脚本可以对用户进行计数并将数据写入mysql db。如果用户来一次,则脚本执行唯一查询,如果用户来一次以上,则执行非唯一查询。如果用户代理是bot并且出现一次,则脚本会对bot进行唯一查询,如果多次出现,则对bot进行非唯一查询。我的脚本也做了一些聪明的事情,例如:如果管理员在db中标记了ip而不是bot,则下一次脚本将数据标记为非bot ip,然后反向运行,如果管理员在db中标记了ip机器人,则将其改为bot,下次脚本将其像机器人IP。

但是我对这种聪明的东西有疑问。如果我标记了非ip的ip,则下一次脚本将忽略此规则,并以类似ip的bot的方式写入db,然后反向执行。

请帮助我解决这个问题

<?php

    // name of bot ip query in DB
    $DO_BOT = "irobot";
    // name not bot ip query
    $DO_NOT_BOT = "imreal";

    //-- Create connection
    $conishua = new mysqli($sv_name, $ur_name, $multipass, $db_name);
    $conishua->set_charset('utf8');

    //-- Check connection
    if (mysqli_connect_errno()){

        // Just for report error uncheck under this line
        /* echo "Failed to connect to DB: " . mysqli_connect_error(); */
        die (mysqli_error($conishua));
        return false;

    }

    else {

#IF DB EXIST DO A QUERY
            $stmt = $conishua->prepare("SELECT (id) FROM `$tabname` WHERE ip = ?");
            $stmt->bind_param('s', $ip_clear);
            $stmt->execute();
            $rs = $stmt->get_result();
            $data = mysqli_fetch_array($rs);

            //-- If user ip not exist and not bot do uniq query
            if ($rs && $data[0] < 1 && !empty($hash_clear) && isset($hash_clear) && !$BADBOT && !$MY_IP && $BOT_AGENT === FALSE && substr_count($ip_clear, ".") == 3) {

                $stmt = $conishua->prepare("INSERT INTO `$tabname` (ip, agent, date, time, resolution, uniq, hash, device, imreal) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->bind_param("sssssssss", $ip_clear, $agent_clear, $ddate_clear, $ttime_clear, $resolution_clear, $uniq_clear, $hash_clear, $device_clear, $DO_NOT_BOT);
                $stmt->execute();

            }

            //-- If user ip exist and ip's is not bot do non-uniq query and doubble
            else if ($rs && $data[0] >= 1 && !empty($hash_clear) && isset($hash_clear) && $data['date'] != $ddate && $data['time'] != $ttime && $data['hash'] != $hash  && !$BADBOT && !$MY_IP && $BOT_AGENT === FALSE && substr_count($ip_clear, ".") == 3) {

                $stmt = $conishua->prepare("INSERT INTO `$tabname` (ip, agent, date, time, resolution, hash, device, imreal) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->bind_param("ssssssss", $ip_clear, $agent_clear, $ddate_clear, $ttime_clear, $resolution_clear, $hash_clear, $device_clear, $DO_NOT_BOT);
                $stmt->execute();

            }

            //-- If user ip not exist and ip's is bot do uniq query
            else if ($rs && $data[0] < 1 && !empty($hash_clear) && isset($hash_clear) && !$BADBOT && !$MY_IP && $BOT_AGENT === TRUE && substr_count($ip_clear, ".") == 3) {

                $stmt = $conishua->prepare("INSERT INTO `$tabname` (ip, agent, date, time, resolution, uniq, is_bot, hash, device) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->bind_param("sssssssss", $ip_clear, $agent_clear, $ddate_clear, $ttime_clear, $resolution_clear, $uniq_clear, $DO_BOT, $hash_clear, $device_clear);
                $stmt->execute();

            }

            //-- If user ip exist and ip's is bot do non-uniq query
            else if ($rs && $data[0] >= 1 && !empty($hash_clear) && isset($hash_clear) && $data['date'] != $ddate && $data['time'] != $ttime && $data['hash'] != $hash && !$BADBOT && !$MY_IP && $BOT_AGENT === TRUE && substr_count($ip_clear, ".") == 3) {

                $stmt = $conishua->prepare("INSERT INTO `$tabname` (ip, agent, date, time, resolution, is_bot, hash, device) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
                $stmt->bind_param("ssssssss", $ip_clear, $agent_clear, $ddate_clear, $ttime_clear, $resolution_clear, $DO_BOT, $hash_clear, $device_clear);
                $stmt->execute();

            }
    }
?>

0 个答案:

没有答案