Jump to content

ip2long function problem with post


tomindo

Recommended Posts

could someone help me with this code? It works fine without ip2long function? weird

<form method="post" action="test.php"> 
<textarea name="new" cols="40" rows="10"> 
samplehost-1,127.0.0.1
samplehost-2,127.0.0.2
</textarea><br>
<input type="submit" value="Add Servers"> 
</form>
if(isset($_POST['new'])) {
$array= explode("\n",$_POST['new']);
foreach ($array as $ni) {
			$array=explode(',', $ni);
			echo ip2long($array[1]);	
}
}

Link to comment
Share on other sites

Looks to me like you're probably getting a (bool) false back from these addresses, which will show up as nothing in HTML. Try this:

<form method="post" action="<?php echo $PHP_SELF;?>">
<textarea name="new" cols="40" rows="10">
samplehost-1,127.0.0.1
samplehost-2,127.0.0.2
</textarea><br>
<input type="submit" value="Add Servers">
</form>
<?php
if(isset($_POST['new'])) {
$array = explode("\n",$_POST['new']);
foreach ($array as $ni) {
	$test = explode(',', $ni);
	echo ip2long(trim($test[1]))."<br/>";
    }
}
?>

 

I just threw a trim() around the item. I also changed your second variable to '$test' as it looked to me like you were overwriting your original $array with the second explode... but I didn't look at it long enough to be sure. Just make sure you're grabbing an IP address.

 

You might think about using a regular expression... something like this:

 

<form method="post" action="<?php echo $PHP_SELF;?>">
<textarea name="new" cols="40" rows="10">
samplehost-1,127.0.0.1
samplehost-2,127.0.0.2
samplehost-3,999.999.999.999</textarea><br>
<input type="submit" value="Add Servers">
</form>
<?php
if(isset($_POST['new'])) {
$RE = "/(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)/";
foreach (explode("\n",$_POST['new']) as $address) {
	$data = array();
	if (preg_match($RE, $address, $data)) {
		var_dump($data);
		echo $address." = ".ip2long($data[0])."<br />";
	} else {
		echo "Not a valid address!";
	}
}
}
?>

 

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.