【问题标题】:PHP Unidentified Function script [closed]PHP未识别函数脚本[关闭]
【发布时间】:2012-12-21 15:57:15
【问题描述】:

我尝试使用的代码如下,我仔细检查了它和所有内容,它一直说这个错误:致命错误:调用未定义的函数 Votifier(),我不知道问题出在哪里。这是我在这里问的最后手段,我已经在谷歌上搜索了 2 个小时.. 提前致谢。

<?php
if (isset($_COOKIE['votfed']) && $_COOKIE['vofted'] == 'true') {
    exit();
} else {
    mysql_connect("", "", "")or die("cannot connect");
    mysql_select_db("")or die("cannot select DB");
    $result = mysql_query('SELECT * FROM servers WHERE id = "' . $_GET["server"] . '"');
    while ($row = mysql_fetch_array($result)) {
        $public_key  = $row['votifier_key'];
        $server_ip   = $row['ip'];
        $server_port = $row['votifier_port'];
        $username    = 'USERNAME';
    }
    $username = preg_replace("/[^A-Za-z0-9_]+/", '', $username);
    if (Votifier($public_key, $server_ip, $server_port, $username)) {
        echo 'Success!';
    } else {
        echo 'Error!';
    }
    ini_set('error_reporting', E_ALL);
    function Votifier($public_key, $server_ip, $server_port, $username) {

        $public_key = wordwrap($public_key, 65, "\n", true);
        $public_key = <<<EOF
-----BEGIN PUBLIC KEY-----
$public_key
-----END PUBLIC KEY-----
EOF;
        $address    = $_SERVER['REMOTE_ADDR'];
        $timestamp  = time();
        $string     = "VOTE\MC-ServerLists.com\n$username\n$address\n$timeStamp\n";
        $leftover   = (256 - strlen($string)) / 2;
        while ($leftover > 0) {
            $string .= "\x0";
            $leftover--;
        }
        openssl_public_encrypt($string, $crypted, $public_key);
        $socket = fsockopen($server_ip, $server_port, $errno, $errstr, 3);
        if ($socket) {
            fwrite($socket, $crypted);

            return true;
        } else
            return false;
    }

    mysql_connect("", "", "")or die("cannot connect");
    mysql_select_db("")or die("cannot select DB");
    mysql_query('insert into voters (server_id, ipaddress) VALUES ("' . $_GET["server"] . '", "' . $_SERVER['REMOTE_ADDR'] . '")');
}

【问题讨论】:

标签: mysql cookies php minecraft


【解决方案1】:

如果你的代码格式正确,你会看到函数是有条件定义的,因此,它的定义必须在你调用函数之前发生:

<?php
if (isset($_COOKIE['votfed']) && $_COOKIE['vofted'] == 'true') {
    // ...
} else {
    // ...
    $username = preg_replace("/[^A-Za-z0-9_]+/", '', $username);
    if (Votifier($public_key, $server_ip, $server_port, $username)) {
        echo 'Success!';
    } else {
        echo 'Error!';
    }
    function Votifier($public_key, $server_ip, $server_port, $username)
    {
        // ...
    }
    // ...
}
?> 

本质上,您正在执行此代码:

<?php
if( false) {
} else {
    if( foo()) { 
        echo 'Foo!'; 
    }
    function foo() { 
        return true;
    }
}

这应该告诉你 foo() 直到 else 执行之后才被定义,但你在 else 块的开头调用 foo()(在它被定义之前)。

您的函数应该在if 语句之外,如下所示:

<?php
function Votifier($public_key, $server_ip, $server_port, $username)
{
    // ...
}
if (isset($_COOKIE['votfed']) && $_COOKIE['vofted'] == 'true') {
    // ...
} else {
    // ...
    $username = preg_replace("/[^A-Za-z0-9_]+/", '', $username);
    if (Votifier($public_key, $server_ip, $server_port, $username)) {
        echo 'Success!';
    } else {
        echo 'Error!';
    }
    // ...
}

或者,您可以将它放在 else 块的开头,但从可读性的角度来看,我不推荐它。

【讨论】:

  • +1 但次要(迂腐)更正 - 该函数在正确的“范围”中定义,但由于它是有条件地定义的,因此必须先定义它才能使用。如果函数是在全局“范围”中定义的(即在调用时不是有条件的或以其他方式定义的),那么它们在文件中相对于调用代码的位置无关紧要。
  • @Dave - 我认为我解释得不好。谢谢,我已经更新了我的答案。
  • 另外你没有指出关于$timeStamp的未定义变量警告... ;)
猜你喜欢
  • 1970-01-01
  • 2013-01-12
  • 2012-02-04
  • 2019-01-16
  • 2015-05-26
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多