Jump to content

My script just returns true whatever I throw at it!


Malevolence

Recommended Posts

Hey guys,

 

I've made a little script which parses a file called 'banned_ips.log'. The file is in the following format:

 

User IP | Date of Ban | Reason for Ban.

 

it is split between line breaks (\n) and the individual data is seperated between " | " as you can see.

 

I tested it out to see if my parsing bit worked by printing the various array id's e.g. $array_varname[2] and it returned the correct data, so it's not my parsing that is incorrect.

 

I inserted the following lines of data into this file:

 

This is the stuff that banned people can\'t see. *Snigger*

172.159.8.76 | 12052008 | Et Cetera

75.153.847.128 | 13052008 | Blah Blah

268.159.36.76 | 24062008 | For Example

127.4.57.375 | 05022009 | Useless Record

xxx.xxx.xxx.xxx | 07022009 | For Test Purposes

 

Notice 'xxx.xxx.xxx.xxx | 07022009 | For Test Purposes' (as this will be the fake IP to test if true or false)

 

This is my code:

 

 <?php

// Makes a fake IP purely to test if my rubbish coding skills actually work! (This IP is also in the log file)
$user_ip = "xxx.xxx.xxx.xxx";

// Connects to the banned_ips.log file and parses the data into seperate records
$filename = "banned_ips.log";
    $handle = fopen($filename, "rb");
    $cnt = fread($handle, filesize($filename));
$array_cnt = explode("\n", $contents);

// Checks whether the users IP is in the log file. Also checks what Array number/id that is.
$ipchk = false; // Sets ipchk to false by default.
$ipchk = in_array($user_ip, $array_cnt);
$ipchk_id = array_search($user_ip, $array_cnt);

// Retrieves the data to display.
$banid = $array_cnt[$ipchk_id];
$banexplode = explode(" | ", $banid);
$banip = $banexplode[0];
$bandate = $banexplode[1];
$banreason = $banexplode[2];

// If the $ipchk returns true, then the User's IP is in the IP ban list so tell them to smeg off.
if ($ipchk = true) {
	echo 'Your IP ('.$banip.') was banned from this website on '.$bandate.' for reason: "'.$banreason.'".';
}
// If anything else is returned (Which can only really be false), the user's IP is not in the IP ban list, so welcome them warmly.
else {
	echo 'This is the stuff that banned people can\'t see. *Snigger*';
}

    fclose($handle);
?> 

 

My problem is that it just echo's "Your IP () was banned from this website on '' for reason:". First of all, I removed my IP from the log file to see if it had moved to "This is the stuff that banned people can't see. *Snigger*" like it should, and it didn't. I then had a look at what $ipchk and $ipchk_id had returned using print, echo and print_r();, all of which returned absolutely nothing. Of course, I then made an if statement saying:

 

if ($ipchk = true) { 
echo 'IP CHK is TRUE';
}
else {
echo 'IP CHK is FALSE';
}

 

And it returned "IP CHK is TRUE". I removed my IP from the log again and it STILL said "IP CHK is TRUE".

 

I did the same with $ipchk_id (of course replacing true with 4 (the id of my ip in the array)) and the same happened. It returned "IP CHK ID is 4".

 

Can anyone see my error(s)? There may be other things I have done wrong, but I don't know....

 

Thanks in Advance,

Dan.

 

 

Link to comment
Share on other sites

Well that is good observation, thanks but, it still doesn't work.

 

I'll post my current code for ya:

<?php

// Makes a fake IP purely to test if my rubbish coding skills actually work! (This IP is also in the log file)
$user_ip = "xxx.xxx.xxx.xxx";

// Connects to the banned_ips.log file and parses the data into seperate records
$filename = "banned_ips.log";
    $handle = fopen($filename, "rb");
    $cnt = fread($handle, filesize($filename));
$array_cnt = explode("\n", $cnt);

// Checks whether the users IP is in the log file. Also checks what Array number/id that is.
$ipchk = in_array($user_ip, $array_cnt);
$ipchk_id = array_search($user_ip, $array_cnt);

// Retrieves the data to display.
$banid = $array_cnt[$ipchk_id];
$banexplode = explode(" | ", $banid);
$banip = $banexplode[0];
$bandate = $banexplode[1];
$banreason = $banexplode[2];

// If the $ipchk returns true, then the User's IP is in the IP ban list so tell them to smeg off.
if ($ipchk == true) {
	echo 'Your IP ('.$banip.') was banned from this website on '.$bandate.' for reason: "'.$banreason.'".';
}
// If anything else is returned (Which can only really be false), the user's IP is not in the IP ban list, so welcome them warmly.
else {
	echo 'This is the stuff that banned people can\'t see. *Snigger*';
}

    fclose($handle);
?> 

Link to comment
Share on other sites

This line

$ipchk = in_array($user_ip, $array_cnt);

always returns false, because it checks if the user's IP is in the array of lines. The lines have the IP but also the other two things. Here's some code that would work:

 

$ipchk = false;
foreach ($array_cnt as $line) {
     if (strpos($line, $user_ip) === 0) {
          $ipchk = true;
     }
}

 

It loops through the lines in the file and checks if the user's ip is on the current line (starting at position 0).

Link to comment
Share on other sites

Well, it works now (If the IP exists, it will show the correct message) but it just displays the first row in the log file (Meaning an incorrect ip and reason and date). I want that data to correspond to the row that caused the person to be banned:

 

$ipchk_id = array_search($user_ip, $array_cnt);

// Retrieves the data to display.
$banid = $array_cnt[$ipchk_id];
$banexplode = explode(" | ", $banid);
$banip = $banexplode[0];
$bandate = $banexplode[1];
$banreason = $banexplode[2];

 

Basically, I need to find the number of the array line like this: $array_cnt[4] < where 4 is the dynamic number.

 

What array_search is meant to do, is find that number based on the search criteria. It's not doing it's job....

 

print_r($ipchk_id); isn't showing anything. nor is print and echo.

Link to comment
Share on other sites

I'm sure you are learning some good skillls from doing this, so on that note what you are doing is good. But considering almost all IP addresses are dynamic these days, trying to ban someone based on ID is a somewhat futile effort.

Link to comment
Share on other sites

If you're using that foreach loop I gave you, you could just store the line into a variable when you find it:

 

$ipchk = false;
foreach ($array_cnt as $line) {
     if (strpos($line, $user_ip) === 0) {
          $ipchk = true;
          $banid = $line;
     }
}

 

Then you don't have to worry about the array number.

Link to comment
Share on other sites

To address why your script isnt working now - well its the same problem you had earlier, but with a different line:

 

	$ipchk_id = array_search($user_ip, $array_cnt);

 

$user_ip is an ip address, but the values stored in $array_cnt are strings that contain the IP, but also other stuff. As a result, its not finding a match, which returns zero, which is what is passed on to the rest of your code.

Link to comment
Share on other sites

Jeremysr, big big thanks to you, but just to clarify, can you explain what the following actually does?

 

$ipchk = false;

foreach ($array_cnt as $line) {

    if (strpos($line, $user_ip) === 0) {

          $ipchk = true;

          $banid = $line;

    }

}

 

I know that foreach is a loop, but what does it all do?

Link to comment
Share on other sites

It loops through the $array_cnt array. Each iteration it assigns the next element of the array to the variable $line.

 

The if statement checks to see if their ip address is at the beginning of the current line it's checking. strpos() returns the position that the ip address is found. If it isn't found, it returns false. Since 0 and false mean the same thing, I used ===. So if it found the IP address on the current line, it sets $ipck to true and sets $banid to the current line.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.