如何在TS3AntiVPN应用程序中为$ clientList正确编写foreach?

时间:2019-04-28 20:30:10

标签: php

我下载了TS3AntiVPN,但出现错误。我使用安装了Debian 9 Plesk的Linux服务器。

  

PHP警告:为中的foreach()提供了无效的参数   /var/www/vhosts/suspectgaming.de/tsweb.suspectgaming.de/antivpn/bot.php   在第29行

我该如何解决这个问题?

<?php
require("ts3admin.class.php");
$ignore_groups = array('1',);   // now supports one input and array input 
$msg_kick = "VPN"; 
$login_query = "serveradmin"; 
$pass_query = ""; 
$adres_ip = "94.249.254.216";
$query_port = "10011"; 
$port_ts = "9987"; 
$nom_bot = "AntiVPN"; 

$ts = new ts3Admin($adres_ip, $query_port);

if(!$ts->getElement('success', $ts->connect()))  {
      die("Anti-Proxy");
}

$ts->login($login_query, $pass_query);
$ts->selectServer($port_ts);

$ts->setName($nom_bot);

while(true) {

    sleep(1);

    $clientList = $ts->clientList("-ip -groups");

    foreach($clientList['data'] as $val) {

        $groups = explode(",", $val['client_servergroups'] );

        if(is_array($ignore_groups)){

            foreach($ignore_groups as $ig){

                if(in_array($ig, $groups) || ($val['client_type'] == 1)) {

                    continue;   
                }
            }
        }else{

            if(in_array($ignore_groups, $groups) || ($val['client_type'] == 1)) {
                continue;
            }
        }

        $file = file_get_contents('https://api.xdefcon.com/proxy/check/?ip='.$val['connection_client_ip'].'');

        $file = json_decode($file, true);

        if($file['message'] == "Proxy detected.") {
            $ts->clientKick($val['clid'], "server", $msg_kick);
        }
    }
}

?>

1 个答案:

答案 0 :(得分:0)

您可能在第一个数组中缺少数据。您可以添加if语句来检查您的$clientList['data']变量中是否有数据:

if (is_array($clientList['data'])) {


}

或者,您也可能需要检查数组的sizeof();是否大于数字。

if (is_array($clientList['data']) && sizeof($clientList['data']) > 0) {


}

代码

require "ts3admin.class.php";
$ignore_groups = array('1'); // now supports one input and array input
$msg_kick = "VPN";
$login_query = "serveradmin";
$pass_query = "";
$adres_ip = "94.249.254.216";
$query_port = "10011";
$port_ts = "9987";
$nom_bot = "AntiVPN";

$ts = new ts3Admin($adres_ip, $query_port);

if (!$ts->getElement('success', $ts->connect())) {
    die("Anti-Proxy");
}

$ts->login($login_query, $pass_query);
$ts->selectServer($port_ts);

$ts->setName($nom_bot);

while (true) {

    sleep(1);

    $clientList = $ts->clientList("-ip -groups");

    if (is_array($clientList['data']) && sizeof($clientList['data']) > 0) {
        foreach ($clientList['data'] as $val) {

            $groups = explode(",", $val['client_servergroups']);

            if (is_array($ignore_groups)) {

                foreach ($ignore_groups as $ig) {

                    if (in_array($ig, $groups) || ($val['client_type'] == 1)) {

                        continue;
                    }
                }
            } else {

                if (in_array($ignore_groups, $groups) || ($val['client_type'] == 1)) {
                    continue;
                }
            }

            $file = file_get_contents('https://api.xdefcon.com/proxy/check/?ip=' . $val['connection_client_ip'] . '');

            $file = json_decode($file, true);

            if ($file['message'] == "Proxy detected.") {
                $ts->clientKick($val['clid'], "server", $msg_kick);
            }
        }
    } else {
        echo "There might be no data in Client List";
    }
}