Cory94bailly Posted January 7, 2011 Share Posted January 7, 2011 Well I guess I'll jump right into the code.. The function: function isIpaddr ($ipaddr) { if (ereg("^([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3}).([0-9]{1,3})$", $ipaddr, $digit)) { if (($digit[1] <= 255) && ($digit[2] <= 255) && ($digit[3] <= 255) && ($digit[4] <= 255)) { return TRUE; } } return FALSE; } The loop and IF statement: if($_POST['submit_multiple']) { $SubmittedIPs = split("[\n|\r|,|-]", $_POST['IPAddresses']); foreach($SubmittedIPs as $IP) { trim($IP); if(isIpaddr($IP)) { echo $IP." = ".gethostbyaddr($IP)."<br />"; } else { echo "Invalid IP Address<br />"; } } } There's my relevant code.. Now the only problem so far is that I am getting this: 12.34.56.78 = 12-34-56-78.test.hostname.com Invalid IP Address 12.34.56.78 = 12-34-56-78.test.hostname.com Invalid IP Address 12.34.56.78 = 12-34-56-78.test.hostname.com (The IPs and Hostnames are obviously fake..) The problem is that they are valid IP addresses and I've done a few checks to see what's wrong. I've checked the array and it is working fine by splitting up a textbox into an array of IPs and the IPs are fine inside the array. I then tested the script by echoing the output of just the function with each IP, I got all "1"s (TRUE). I just can't figure out why it's displaying both parts of the if statement.. Thanks for any help! Quote Link to comment Share on other sites More sharing options...
MMDE Posted January 7, 2011 Share Posted January 7, 2011 12.34.56.78 = 12-34-56-78.test.hostname.com Invalid IP Address 12.34.56.78 = 12-34-56-78.test.hostname.com Invalid IP Address 12.34.56.78 = 12-34-56-78.test.hostname.com I don't get how it can output that... or does it just output Invalid IP Address every time? Have you tried to output what it actually checks, is it what you expect? Have you tested if the function alone works as you expect? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Wild guess: The $SubmitedIPs array holds an invalid value in every other index. $SubmittedIPs = split("[\n|\r|,|-]", $_POST['IPAddresses']); // Add the next three lines and check the output. echo '<pre>'; print_r($SubmittedIPs); echo '</pre>'; foreach($SubmittedIPs as $IP) { Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 Wild guess: The $SubmitedIPs array holds an invalid value in every other index. The weird thing is that I print_r'd before and it looked fine.. Now you're right. I took out the "\r" from the split pattern and it stopped doing it. I thought \n was used by windows OS and \r was used by others, why is a line break considered \n and \r at the same time? Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 WinD'ohs typically uses \r\n, so if Win is what you're using, it's likely those linefeeds are causing this issue. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 WinD'ohs typically uses \r\n, so if Win is what you're using, it's likely those linefeeds are causing this issue. What would be your suggestion? I took out \r from the pattern and when I look at it with print_r, it looks fine.. But if I use the actual code, they all show "Invalid IP Address" except for the last one. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Off the top of my head, i think I'd look into replacing anything that isn't a number or a decimal point with a space, then replacing any instances of 2 or more spaces with one space, then explode() the resulting string using the spaces as the separators, into an array. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 Off the top of my head, i think I'd look into replacing anything that isn't a number or a decimal point with a space, then replacing any instances of 2 or more spaces with one space, then explode() the resulting string using the spaces as the separators, into an array. I'm trying to make it separate by line breaks, commas, dashes, and spaces would be nice too I don't understand why if I print_r it, it looks normal.. But while checking the IPs, it says they're all invalid except for either the first or last... Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 It may be that the values have leading and/or trailing white space. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 It may be that the values have leading and/or trailing white space. That's why I'm using trim() on each IP before it's 'analyzed' Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Ah, yes. I guess I only would have had to scroll up to see that . . . BTW, have you considered using filter_var( $var, FILTER_VALIDATE_IP) rather than the isIpaddr() function? Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 Ah, yes. I guess I only would have had to scroll up to see that . . . BTW, have you considered using filter_var( $var, FILTER_VALIDATE_IP) rather than the isIpaddr() function? I had no idea it existed until you told me Well, now I'm getting: Fatal error: Call to undefined function filter_var() in /www/path/to/htdocs/ip.php on line 35 Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 My updated code: if($_POST['submit_multiple']) { $SubmittedIPs = explode("\n", $_POST['IPAddresses']); echo '<pre>'; print_r($SubmittedIPs); echo '</pre>'; foreach($SubmittedIPs as $IP) { trim($IP); if(isIpaddr($IP)) { echo $IP." = ".gethostbyaddr($IP)."<br />"; } else { echo "Invalid IP Address<br />"; } } } My updated test: Array ( [0] => 12.34.56.78 [1] => 12.34.56.78 [2] => 12.34.56.78 ) Invalid IP Address Invalid IP Address 12.34.56.78 = 12-34-56-78.test.hostname.com The spacing of the array is exactly the same for each one. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 7, 2011 Share Posted January 7, 2011 Sounds like you may be running a version before 5.2.0? That's when filter_var() was introduced. Quote Link to comment Share on other sites More sharing options...
Cory94bailly Posted January 7, 2011 Author Share Posted January 7, 2011 Sounds like you may be running a version before 5.2.0? That's when filter_var() was introduced. I guess so.. But it's a free testing host so I have no control over anything. 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.