PNewCode Posted December 14, 2023 Share Posted December 14, 2023 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 ?> Quote Link to comment Share on other sites More sharing options...
Solution kicken Posted December 14, 2023 Solution Share Posted December 14, 2023 (edited) 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 December 14, 2023 by kicken wrong character Quote Link to comment Share on other sites More sharing options...
PNewCode Posted December 14, 2023 Author Share Posted December 14, 2023 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! Quote Link to comment Share on other sites More sharing options...
gizmola Posted December 20, 2023 Share Posted December 20, 2023 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.