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-&gt; userCanVote($ this-&gt; getIP(),$ uuid); 时,我总是只需 1 作为返回值尽管在课程结束时添加了一个return语句。有人可以帮帮我吗?还有另一个文件,我实际上使用这个类及其功能,但我不认为有必要在这里发布。
谢谢!
答案 0 :(得分:2)
因为你回复了布尔值。
BTET
将打印<?php
echo true;
?>
。
如果你真的需要打印“真实”字样。或者&#39; false&#39;,你应该这样做:
1
答案 1 :(得分:-1)
尝试此更改。
function userCanVote($ip,$uuid) {
try{ $this->db->prepare('SELECT COUNT(*) FROM `voterlist` WHERE `ipv4`=:ip AND `votetime`>DATE_SUB(NOW(), INTERVAL 1 DAY) AND `uuid`=:uuid');
$this->db->bindParam(':ip',$ip);
$this->db->bindParam(':uuid',$uuid);
$this->db->execute();
$result = $this->db->fetchColumn();
return($result>0 ? false : true);
}catch(Exception $e){
die("<div class='container'><span class='alert error'><b>Error in hasUserVoted()</b><br>".$e."</span></div>");}
}