Malevolence Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/ Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 The operator to use for comparison is "==" not "=". Ken Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494567 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 All "==" has done is reversed the return, however the same problem is occuring. I deleted the IP from the log and it STILL stays the same. Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494571 Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 Looks like you meant the $cnt and $contents on lines 9 and 10 to be the same variables. Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494573 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494588 Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494596 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494612 Share on other sites More sharing options...
haku Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494618 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 I know, but It is helping me learn about dealing with arrays, files etc. Thats all that really matters, plus I've been asked to do this. You'd be surprised how many people have static IP's in my forum........ Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494621 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 Can anyone help me? look up 2 posts ago. Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494626 Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494631 Share on other sites More sharing options...
haku Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494633 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494639 Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494642 Share on other sites More sharing options...
Malevolence Posted March 18, 2008 Author Share Posted March 18, 2008 For some reason, I'm unable to set this topic to 'solved'; I can't even edit my first post............. Thanks for your help, Dan. PS: How are you colour coding your php on here? It used to happen in [ code ] [ /code ] but now it's all grey... Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494645 Share on other sites More sharing options...
Jeremysr Posted March 18, 2008 Share Posted March 18, 2008 Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494650 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 if you use the tags, start your snippet with <?php instead of <? Ken Link to comment https://forums.phpfreaks.com/topic/96646-my-script-just-returns-true-whatever-i-throw-at-it/#findComment-494715 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.