Jump to content

Php Mysql IP Ban error in script


Go to solution Solved by kicken,

Recommended Posts

Hello everyone.

I got this script from a friend that told me he got it somewhere online so he doesn't know how to fix it.
This is a script to ban users if an IP address is listed in a database.
That part works. If the IP address is added to the database, then the visitor sees "Access denied!"
However, if someone ISN'T listed (their ip address I mean) then the page just says
"Uncaught Error: Undefined constant "�" home-iptest.php on line 1" which is the code
 

<?php require_once('ip/ip-access.php'); ?>

However, if the ip address is listed in the database then it successfully gives them the access denied test on the page

I see in the script there is a place to put the allow access logic, however I don't know what logic or code to put there. Any assistance is appreciated.
Here is that script

 

<?php

error_reporting(E_ALL);
ini_set('display_errors', '1');


$servername = "Removed for posting";
$username = "Removed for posting";
$password = "Removed for posting";
$dbname = "Removed for posting";

// Get the user's IP address
$userIP = $_SERVER['REMOTE_ADDR'];

// Create a database connection
$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Check if the user's IP exists in the database
$checkQuery = "SELECT * FROM ip WHERE ip = '$userIP'";
$result = $conn->query($checkQuery);

if ($result->num_rows > 0) {
    // User's IP exists in the database, deny access
    echo "Access denied!";
    // You may redirect to an error page or perform other actions here
    exit; // Stop further execution of the page
}

$conn->close();

// If the user's IP does not exist in the database, allow access to the webpage
// Your webpage logic goes here
?>

 

Link to comment
https://forums.phpfreaks.com/topic/317534-php-mysql-ip-ban-error-in-script/
Share on other sites

  • Solution
20 minutes ago, PNewCode said:

"Uncaught Error: Undefined constant "�" home-iptest.php on line 1"

Your file has a invalid character in it that needs to be removed.  Try deleting the line and re-typing it.  Don't copy/paste or you'll copy/paste the character as well.

 

Edited by kicken
wrong character
16 minutes ago, kicken said:

Your file has a byte-order-mark in it that needs to be removed.  Try deleting the line and re-typing it.  Don't copy/paste or you'll copy/paste the BOM as well.

Thank you. Yes there was a space in it that was bad. Now I'm getting issues with having the include_once script that has always been there for some reason.  When I take out the top line that is in the post, there is no issues. But thats a different problem. You're solution worked for the error I was getting. Thank you! :)

Make sure that none of your scripts have an end php tag in them.  It is not needed and can often result in output being started accidentally.

 

<?php


// stuff

?>   // <--- remove these from the end of all .php scripts.

 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.