meltingpoint Posted September 6, 2010 Share Posted September 6, 2010 Object- Take array of IP addresses and add up all occurrences of a particular IP address. //$item is an array of IP addresses foreach ($item as $key => $unique_ip) { if($unique_ip == $ip) $a++; } echo $a; I get a blank page. What am I missing? Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/ Share on other sites More sharing options...
Hypnos Posted September 6, 2010 Share Posted September 6, 2010 Looks right. Did you set $ip? Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108023 Share on other sites More sharing options...
meltingpoint Posted September 6, 2010 Author Share Posted September 6, 2010 Yes- the $ip is set. What I am attempting to do is I have a text file log that logs every submission and if multiple submissions are found from the same IP- it blocks further attempts. But- it is not working as I have written. Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108026 Share on other sites More sharing options...
Hypnos Posted September 6, 2010 Share Posted September 6, 2010 Try var_dump()ing $unique_ip and $ip. Your code looks right. Something must be wrong with your variables. foreach ($item as $key => $unique_ip) { echo "I'm comparing this IP:\n"; var_dump($unique_ip); echo "To this:\n"; var_dump($ip); if($unique_ip == $ip) $a++; } echo $a; Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108028 Share on other sites More sharing options...
meltingpoint Posted September 6, 2010 Author Share Posted September 6, 2010 $ip = $_SERVER['REMOTE_ADDR']; $a = ""; foreach ($subip as $key => $unique_ip) { if($unique_ip == $ip) $a++; } echo $a; This more accurately depicts my script. Additionally- the purpose is to count the number of times the IP address is in the submission log so that I can stop further submissions. The code you gave does echo out properly but does not increment $a Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108032 Share on other sites More sharing options...
Hypnos Posted September 7, 2010 Share Posted September 7, 2010 Your code is right. Only thing I can think of is if your stored IPs have extra whitespace or $subip isn't set correctly. Can you post some of the output from the var_dump code I posted. EDIT: Also if you're going to declare $a, you should may want to set it to 0. Not "". Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108059 Share on other sites More sharing options...
meltingpoint Posted September 7, 2010 Author Share Posted September 7, 2010 Hypnos-------Exellent idea. I added the trim() to the $unique_ip and it worked perfectly! I also too your advise and made $a set to 0. The final code that worked is.... $ip = $_SERVER['REMOTE_ADDR']; $a = "0"; foreach ($subip as $key => $unique_ip) { if(trim($unique_ip) == $ip) $a++; } echo $a; Now I can take $a and compare it against X number of submissions. IF it is over- I will kill the script and ban that IP. Much thanks! Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108065 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2010 Share Posted September 7, 2010 array_count_values would directly get a count of each existing ip address. You can then simply test if the ip address has an entry (or not) in the resulting array and get that count. Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108070 Share on other sites More sharing options...
meltingpoint Posted September 7, 2010 Author Share Posted September 7, 2010 Problem that I had with array_count_values() is that I want to allow X number of submissions before the script would stop them from submitting again. I could not figure how to do it with the array_count_values(). To get a one time occurrence I could also have used in_array() to tell me if it was contained in the array itself. If you know of a way to test if "X" number of occurrences of a particular array value have occurred in an array- I would be very interested. Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108076 Share on other sites More sharing options...
Hypnos Posted September 7, 2010 Share Posted September 7, 2010 This should work. Haven't tested it though. <?php $ip = $_SERVER['REMOTE_ADDR']; $a = 0; $subip_count = array_count_values($subip); if(@$subip_count[$ip]) { $a = $subip_count[$ip]; } echo $a; Basically array_count_values() is going to give you a massive array like this: ['127.0.0.1'] => 3, ['127.0.0.2'] => 10, etc. Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108083 Share on other sites More sharing options...
PFMaBiSmAd Posted September 7, 2010 Share Posted September 7, 2010 I would get rid of the nasty @ - if(isset($subip_count[$ip])) { Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108084 Share on other sites More sharing options...
Hypnos Posted September 7, 2010 Share Posted September 7, 2010 Yeah, if you have display errors off (or warnings out of your error level) you don't need it. Just stops the warning messages that you'll get with non-existent keys. Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108085 Share on other sites More sharing options...
meltingpoint Posted September 7, 2010 Author Share Posted September 7, 2010 Hypnos--- inserted your code into my script and it simply echo's- "0". <?php $ip = trim($_SERVER['REMOTE_ADDR']); $subip = array("192.168.1.1","192.168.1.2","192.168.1.3","192.168.1.4"); $a = 0; $subip_count = array_count_values($subip); if(@$subip_count[$ip]) { $a = $subip_count[$ip]; } echo $a; ?> Not sure how it counts and compares the number of times a particular IP address is in an array? Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108092 Share on other sites More sharing options...
Hypnos Posted September 7, 2010 Share Posted September 7, 2010 Should be right. I just tested it. I put in 192.168.1.1 for $ip and it gave me 1. array_count_values will create a new array with the values and a count for the amount of times they are in the array. Here's an example. <?php $subip = array("192.168.1.1","192.168.1.1"); $subip_count = array_count_values($subip); var_dump($subip_count); Will give: array(1) { ["192.168.1.1"]=> int(2) } Because 192.168.1.1 is twice. So when this is called: echo $subip_count['192.168.1.1']; It will give "2". Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108105 Share on other sites More sharing options...
meltingpoint Posted September 7, 2010 Author Share Posted September 7, 2010 You're correct- silly me..................forgot that $ip = trim($_SERVER['REMOTE_ADDR']); would give my actual server and not the made up ones I was using. Nice- Thanks a lot for all the help. Cheers- Quote Link to comment https://forums.phpfreaks.com/topic/212705-if-statement-in-foreach-loop/#findComment-1108118 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.