I am having some issues with a bit code I wrote. Here is the code:
require "code/core.php";
$ip = $_POST['ip'];
function getIPAddress() {
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = getIPAddress();
$stmt = $db_core->prepare('SELECT COUNT(*) FROM downloads WHERE ip = ?');
$stmt->bind_param('s', $ip);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
if($count == 0) {
$stmt = $db_core->prepare('INSERT INTO downloads (ip) VALUES (?)');
$stmt->bind_param('s', $ip);
$stmt->execute();
} else {
$error = "<span class='error_msg'>You can only download once every 24 hours per IP</span>";
echo $error;
exit;
}
Okay, basically I am getting the user’s IP. Then checking the database to see if it already exists, and throw an error if it does. That part sort of works, except I can’t get the error message to display properly and it thinks the error var is a URL and prints it in the address bar. I have no idea why…
Then if the IP does NOT exist in the database, then insert it into the “downloads” table. But this throws the following error:
Fatal error: Uncaught Error: Call to a member function bind_param() on bool in
/var/www/vhosts/xxx.net/httpdocs/xx/xx/down.php:34 Stack trace: #0 {main} thrown in
/var/www/vhosts/xxx.net/httpdocs/xxx/down.php on line 34
What the heck is going on? The insert command works if I leave out the IP exists check. Is it because I have two identical “bind_param” commands? Any help/tips would be most appreciated!!