PDA

View Full Version : IP Address Validation in JS to PHP


John_Saunders
09-12-2002, 03:27 PM
I found the following IP Address validator in Javascript but I would like it to be in PHP. Does anybody know how to convert it into PHP? Or know of a script like this already written in PHP?

What I would like it to do is just display an error if there is a problem on the next page, instead of saying 'valid ip address'.

<html>
<head>
<!--
This file retrieved from the JS-Examples archives
http://www.js-examples.com
100s of free ready to use scripts, tutorials, forums.
Author: JS-Examples - http://www.js-examples.com/
-->

<script>
function verifyIP (IPvalue) {
errorString = "";
theName = "IPaddress";

var ipArray=IPvalue.split(".");

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(4)';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(5)';
if (!ipArray||!ipArray[0]||!ipArray[1]||!ipArray[2]||!ipArray[3]||ipArray.length>4)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.(1)';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.(2)';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.(3)';
i = 4;
}
}
}
extensionLength = 3;
if (errorString == "")
alert ("That is a valid IP address.(0)");
else {
alert (errorString);
return false;
}
return false;
}




</script>

</head>
<body>

This will validate the input box's value to be an IP number.
<form name=f1>
IP Address:<INPUT type="text" id=txtIPAddress value="255.255.255.255">
<BR>
<input type=text name=result value="?">
<INPUT type="button" value="IPvalue"
onclick= "document.f1.result.value=verifyIP(document.f1.txtIPAddress.value)">
</form>


</body>
</html>



John

Ökii
09-13-2002, 07:27 PM
Ummm, you're asking people to input their IP addresses? Three quarters of the surfing public probably wouldn't know an IP address if it sneaked up behind them and stole their underpants.

Wouldn't just recording the IP through scripting work a tad better?

John_Saunders
09-13-2002, 07:31 PM
Actually, it's not the IP Address of the visitor's connection. It's for Network Administrators to enter so the only people who would be filling out the form would be someone who knows what and IP address is.


John

downsize
09-14-2002, 09:37 PM
just convert it. it will have to post instead of the 'onclick' method - but pretty much all the code can be transfered over to php (except for concating the strings, you have to change the +'s to .'s)

give it a shot and let us know how it goes :-}

downsize
09-14-2002, 10:55 PM
I am not sure if this is what you are looking for but I converted it... enjoy:



<html>
<head>
<?php


function verifyIP ($txtIPAddress) {
$errorString = "";
$theName = "IPaddress";
$ipArray=explode(".", $txtIPAddress);

if ($txtIPAddress == "0.0.0.0"){
$errorString = $errorString . $theName . ': '.$txtIPAddress.' is a special IP address and cannot be used here.(4)';
}
else if ($txtIPAddress == "255.255.255.255"){
$errorString = $errorString . $theName . ': '.$txtIPAddress.' is a special IP address and cannot be used here.(5)';
}
if (!$ipArray||!$ipArray[0]||!$ipArray[1]||!$ipArray[2]||!$ipArray[3]||sizeof($ipArray)>4){
$errorString = $errorString . $theName . ': '.$txtIPAddress.' is not a valid IP address.(1)';
}
else {
for ($i = 0; $i < 4; $i++) {
$thisSegment = $ipArray[$i];
if ($thisSegment > 255) {
$errorString = $errorString . $theName . ': '.$txtIPAddress.' is not a valid IP address.(2)';
$i = 4;
}
if (($i == 0) && ($thisSegment > 255)) {
$errorString = $errorString . $theName . ': '.$txtIPAddress.' is a special IP address and cannot be used here.(3)';
$i = 4;
}
}
}
$extensionLength = 3;
if ($errorString == "")
print ("That is a valid IP address.(0)");
else {
print ($errorString);
return false;
}
return false;
}
?>

</head>
<body>
<?
if(!isset($txtIPAddress)){
?>
<h5>This will validate the input box's value to be an IP number.</h5>
<form action=<? echo $_SERVER['PHP_SELF']?> method="post">
IP Address:<INPUT type="text" name="txtIPAddress" value="255.255.255.255">
<BR>
<input type=text name="result" value="<?echo $txtIPAddress?>">
<INPUT type="submit" value="IPValue">
</form>
<a href="<? echo $_SERVER['PHP_SELF']?>">clear form</a>
<?
}else{
?>
<h5>This will validate the input box's value to be an IP number.</h5>
<? verifyIP($txtIPAddress); ?>
<form action=<? echo $_SERVER['PHP_SELF']?> method="post">
IP Address:<INPUT type="text" name="txtIPAddress" value="255.255.255.255">
<BR>
<input type=text name="result" value="<?echo $txtIPAddress?>">
<INPUT type="submit" value="IPValue">
</form>
<a href="<? echo $_SERVER['PHP_SELF']?>">clear form</a>
<?
}

?>
</body>
</html>

John_Saunders
09-16-2002, 08:23 PM
Hey Downsize,

Thanks a lot for taking the time to convert the js to php. I really appreciate it.


Regards,

John

downsize
09-16-2002, 10:17 PM
I hope it serves your purpose or at least gives an example to do whatever it is you really need.