Jump to content

Problem with an IF statement?


Cory94bailly

Recommended Posts

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! :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :P

 

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...

Link to comment
Share on other sites

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 :P

 

Well, now I'm getting:

Fatal error: Call to undefined function filter_var() in /www/path/to/htdocs/ip.php on line 35
Link to comment
Share on other sites

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.

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.