为什么函数userCanVote()总是返回1


Why does function userCanVote() always return 1

key.php中的完整代码:

    <?php
class Vote {
    public $db;
    public function __construct() {
        try{
            $this->db = new PDO('mysql:host=localhost;port=3306;dbname=voting', 'root', '');
            $sql = "CREATE TABLE IF NOT EXISTS `voterlist` (
            `uuid` varchar(32) NOT NULL,
            `ipv4` varchar(15),
            `votetime` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            PRIMARY KEY (`uuid`))
            CHARACTER SET utf8 COLLATE utf8_general_ci";
            $test = $this->db->query($sql);
        }catch(PDOException $e){
            die("<div class='container'><span class='alert error'>Could not connect!</span></div>");
        }
    }
  function userCanVote($ip,$uuid) {
      try{ $result = $this->db->query('SELECT * FROM voterlist WHERE ipv4="$ip" && votetime>timestampadd(day,-1,now()) && uuid="$uuid"');
          return(!$result->fetchColumn()>0 ? true : false);
      }catch(Exception $e){
      die("<div class='container'><span class='alert error'><b>Error in hasUserVoted()</b><br>".$e."</span></div>");}
  }
    function getIP() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else {
            return $_SERVER['REMOTE_ADDR'];
        }
    } 
    function getUserInfo($x,$y) {
        $user = json_decode(file_get_contents("http://mcapi.de/api/user/".$x));
        return ($y=="name"?$user->username:$user->uuid);
    }
    function execVote($uuid) {
        try {
            if ($this->userCanVote($this->getIP(),$uuid)) {
              echo $this->userCanVote($this->getIP(),$uuid);
                $ip=$this->getIP();
                $name = $this->getUserInfo($uuid,"name");
                $this->db->query("INSERT INTO voterlist (uuid,ipv4) VALUES ('$uuid','$ip')");
                Votifier($name);
            } else {
                echo "You can only vote once every 24 hours!";
            }
        } catch (Exception $e) {
            die("<div class='container'><span class='alert error'>Failed to prepVote() the DB!</span></div>");
        }
    }
}
define("SERVER_IP", "localhost");
define("SERVER_PORT", 8192);
define("PUBLIC_KEY",
"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApYVGI2Tk5/8s58bNRdAxJg/12A/xqL8z2NjJI9+fpKH6rPSPn4mjfVJoOWkj0sXQNeE97Y6MrVPUqflBQeCxGwBzbRlb6nyApqhkzRB9pkIn9BGGsYT9CaZcQGmZafY/9Bxz4rpIf6R9NgRfs4HzuNgAxprAi0slUJ2M2SK/VSlNX5OhwRbBTRrUWfUDJMWNUGNvW8XBFd2foyIJ/mdNbbERF8CZZRVpOpq3qe/I04Z6FRt7VfZTWyVSPsHwlbYaZafgql5Szr7SCMcxYQBMMcxsWZDn8uX3RwJbNYcq3Yow/mS9fKTLKdKDxK+lRBU2CfOAKjfvYx/ak04+rUTRCwIDAQAB"
);
// ---------------------------------------------------------------
function Votifier($username) {
$public_key2 = wordwrap(PUBLIC_KEY, 65, "'n", true);
$public_key = <<<EOF
-----BEGIN PUBLIC KEY-----
$public_key2
-----END PUBLIC KEY-----
EOF;
$address = $_SERVER['REMOTE_ADDR']; // user ip
$timeStamp = time();                // timestamp
$string = "VOTE'nNikolovDZN.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;
}
?>

执行echo$this->userCanVote($this->getIP(),$uuid)时,我总是简单地得到1作为返回值,尽管在类的末尾添加了一个return语句。有人能帮我吗?在另一个文件中,我实际使用了这个类及其函数,但我认为没有必要在这里发布。

谢谢!

因为您正在回显布尔值。

<?php
    echo true;
?>

将打印CCD_ 1。

如果你真的需要打印单词"true"或"false",你应该这样做:

echo $this->userCanVote($this->getIP(), $uuid) ? 'true' : 'false';