我正在尝试$.ajax
调用PHP文件(microsubs.php
),如果此人处于IP范围内,我需要成功调用。以下是PHP代码:
$ip = $_SERVER['REMOTE_ADDR'];
$public_ip_ranges = array();
$range = (object) array();
$range->name = '123 Media';
$range->lower = '10.1.0.0';
$range->upper = '10.1.255.255';
$public_ip_ranges[] = $range;
$range = (object) array();
$range->name = '123 media 3';
$range->lower = '141.228.0.0';
$range->upper = '141.228.255.255';
$public_ip_ranges[] = $range;
$range = (object) array();
$range->name = 'vaerrrrrr';
$range->lower = '146.127.0.0';
$range->upper = '146.127.255.255';
$public_ip_ranges[] = $range;
if (($lngIP=ip2long($ip)) < 0) $lngIP += 4294967296;
foreach ($public_ip_ranges as $ip_range) {
if (($lngLow=ip2long($ip_range->lower)) < 0) $lngLow += 4294967296;
if (($lngHigh=ip2long($ip_range->upper)) < 0) $lngHigh += 4294967296;
if($lngIP >= $lngLow and $lngIP <= $lngHigh) {
?>
以下是我的Ajax电话:
$.ajax({
url: "/microsub.php",
method: 'GET',
success: function (){
}
});
我在success
回调中放入什么来查看此人是否在PHP文件中运行的IP范围函数中?
答案 0 :(得分:1)
在PHP中回显您的结果:例如1
如果IP在范围内:
if($lngIP >= $lngLow and $lngIP <= $lngHigh){
echo 1;
}else{ echo 0}
exit;
在Javascript中,阅读结果并按照您的喜好执行操作:
$.ajax({
url: "/microsub.php",
method: 'GET',
success: function (data){
var inRange = (+data===1);
}
});