判断IP是否合法的正则:<?php
$str = "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";
echo $str;
if (ereg($str,"127.0.0.1")) {
echo "通过初步检查";
}
?>你那个替换为这个吧。
$str = "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$";
echo $str;
if (ereg($str,"127.0.0.1")) {
echo "通过初步检查";
}
?>你那个替换为这个吧。
用preg_*函数.比如说preg_match
照二楼的改成preg_*的吧。
<?php
$str = "/^((2[01]\d|22[0-3]|1\d{2}|[1-9]\d|[1-9])\.)((25[0-5]|2[0-4]\d|1?\d{1,2})\.){2}((25[0-5]|2[0-4]\d|1?\d{1,2}))$/";
if (preg_match($str,"127.0.0.1")) { echo "通过初步检查"; }
?>
<?php
$ips = "$_POST[cxip]";
if (preg_match("'^((2[01]\d|22[0-3]|1\d{2}|[1-9]\d|[1-9])\.)((25[0-5]|2[0-4]\d|1?\d{1,2})\.){2}((25[0-5]|2[0-4]\d|1?\d{1,2}))$'",$ips)){
$link_id = mysql_connect("127.0.0.1","sa","sasadadadadadada");
mysql_select_db("ipdatabase");
mysql_query("set names latin1"); $getip = explode(".",$ips);
echo "您查询的ip地址是: "."$ips";
echo "<br><br><br>";
$formatip = ($getip[0] << 24) + ($getip[1] << 16) + ($getip[2] << 8) + $getip[3];
$str = "select * from ip_test where $formatip between beginipaddr and endipaddr";
$result = mysql_query($str,$link_id);
while ($row = mysql_fetch_array($result)) {
echo "<strong>$row[3] "."$row[4]</strong>";
}
}
else
echo "您填写的ip地址不正确,请重新填写";
?>
以上我的代码有什么问题么,为什么每次执行不能echo出ip的物理地址,只能显示出我输入的ip