如何比较两个PhP变量中包含的两个Ip地址的前三个八位组
我有两个包含两个ipv4地址的php变量,我需要比较前三个八位字节,如果匹配则返回true,如果不匹配则返回false。非常感谢对编写代码块的帮助如何比较两个PhP变量中包含的两个Ip地址的前三个八位组,php,Php,我有两个包含两个ipv4地址的php变量,我需要比较前三个八位字节,如果匹配则返回true,如果不匹配则返回false。非常感谢对编写代码块的帮助 <?php include('adodb/adodb.inc.php'); mysql_connect("173.86.45,9","abcd","1236"); mysql_select_db("vc"); $pl=mysql_query("SELECT stat_ip from Hasoffers"); $count=mysql_num_r
<?php
include('adodb/adodb.inc.php');
mysql_connect("173.86.45,9","abcd","1236");
mysql_select_db("vc");
$pl=mysql_query("SELECT stat_ip from Hasoffers");
$count=mysql_num_rows($pl);
while($row=mysql_fetch_array($pl))
{
$stat_ip=$row['stat_ip'];
echo sec($stat_ip)."<br>";
}
function sec($stat_ip)
{
$result = mysql_query("select stat_ip from Hasoffers where stat_ip ='".$stat_ip."'");
if(condition to check if the octets match)
{
//i need to write the condition if within the table Hasoffers, there are more than 2 'stat_ip'(column) values, having the same 3 octets.
printf("true");
}
else
{
printf("false, octets don't match");
}
return $num_rows;
}
?>
使用“.”分解将它们转换为数组,并比较两个数组的第一个索引。使用“.”分解将它们转换为数组,并比较两个数组的第一个索引。使用以下代码:
$ipOne = "192.168.1.1";
$ipTwo = "192.168.1.2";
$ipOneParts = explode(".", $ipOne);
$ipTwoParts = explode(".", $ipTwo);
if(($ipOneParts[0] == $ipTwoParts[0]) &&
($ipOneParts[1] == $ipTwoParts[1]) &&
($ipOneParts[2] == $ipTwoParts[2])){
return true;
} else {
return false;
}
使用此代码:
$ipOne = "192.168.1.1";
$ipTwo = "192.168.1.2";
$ipOneParts = explode(".", $ipOne);
$ipTwoParts = explode(".", $ipTwo);
if(($ipOneParts[0] == $ipTwoParts[0]) &&
($ipOneParts[1] == $ipTwoParts[1]) &&
($ipOneParts[2] == $ipTwoParts[2])){
return true;
} else {
return false;
}
实现这一点的简单方法是:
$ip1 = '192.168.0.1';
$ip2 = '192.168.0.2';
$ip1 = explode('.', $ip1);
$ip2 = explode('.', $ip2);
if ($ip1[0]==$ip2[0] && $ip1[1]==$ip2[1] && $ip1[2]==$ip2[2]) {
//your code here
}
编辑:
尝试用这个函数替换您的sec()函数(阅读注释),并对其进行编辑
function sec($stat_ip)
{
$octets = explode('.', $stat_ip);
$first_three = $octets[0].'.'.$octets[1].'.'.$octets[2].'.'; //this looks like 192.168.0.
$result = mysql_query("SELECT stat_ip from Hasoffers where stat_ip LIKE '".$first_three."%'"); //this gives you all ip's starting with the current ip
if (mysql_num_rows($result)>1)
{
//we have more than one ip starting with current ip
//do something here
}
else
{
//result returns 1 or 0 rows, no matching ip's
}
//return $something;
}
实现这一点的简单方法是:
$ip1 = '192.168.0.1';
$ip2 = '192.168.0.2';
$ip1 = explode('.', $ip1);
$ip2 = explode('.', $ip2);
if ($ip1[0]==$ip2[0] && $ip1[1]==$ip2[1] && $ip1[2]==$ip2[2]) {
//your code here
}
编辑:
尝试用这个函数替换您的sec()函数(阅读注释),并对其进行编辑
function sec($stat_ip)
{
$octets = explode('.', $stat_ip);
$first_three = $octets[0].'.'.$octets[1].'.'.$octets[2].'.'; //this looks like 192.168.0.
$result = mysql_query("SELECT stat_ip from Hasoffers where stat_ip LIKE '".$first_three."%'"); //this gives you all ip's starting with the current ip
if (mysql_num_rows($result)>1)
{
//we have more than one ip starting with current ip
//do something here
}
else
{
//result returns 1 or 0 rows, no matching ip's
}
//return $something;
}
使用strrpos
和substr
功能的解决方案:
$ip1 = '192.168.10.121';
$ip2 = '192.168.10.122';
// the position of the last octet separator
$last_dot_pos = strrpos($ip1, '.');
$is_matched = substr($ip1, 0, $last_dot_pos) == substr($ip2, 0, $last_dot_pos);
var_dump($is_matched);
输出:
bool(true)
使用strrpos
和substr
功能的解决方案:
$ip1 = '192.168.10.121';
$ip2 = '192.168.10.122';
// the position of the last octet separator
$last_dot_pos = strrpos($ip1, '.');
$is_matched = substr($ip1, 0, $last_dot_pos) == substr($ip2, 0, $last_dot_pos);
var_dump($is_matched);
输出:
bool(true)
显示包含两个ipv4地址的变量以快速获取help@RomanPerekhrest我有一个表Hasoffers,其中包含一列stat_ip(包含ip地址)。我需要在同一列中查找是否存在重复的ip地址(仅通过比较前三个八位位组)。因此,如果我理解正确,您想要一个前三个八位位组相同的ip列表,对吗?@NikolayGanovski,是的,对。显示包含两个ipv4地址的变量以快速获得答案help@RomanPerekhrest我有一张桌子,包含列stat_ip(包含ip地址)。我需要在同一列中查找是否存在重复的ip地址(仅通过比较前三个八位字节)。因此,如果我理解正确,您想要一个前三个八位字节相同的ip列表,对吗?@NikolayGanovski,是的。请您参考代码,我已在上面发布。我需要比较Hasoffers表中名为stat_ip的同一列中包含的3个八位ip地址。您在$result中获取的查询将返回以$stat_ip开头的ip,但我只需要返回以前三个子网开头的值。在这个过程中,$first_3;在哪里使用?你能帮我一下吗?对不起,我弄错了。在查询中,而不是stat_ip应该是前三名,检查编辑后的答案你可以参考代码吗,我已经在上面发布了。我需要比较Hasoffers表中名为stat_ip的同一列中包含的3个八位ip地址。您在$result中获取的查询将返回以$stat_ip开头的ip,但我只需要返回以前三个子网开头的值。在这个过程中,$first_3;在哪里使用?你能帮我一下吗?对不起,我弄错了。在查询中,而不是stat_ip应该是前三个,检查编辑的答案