【问题标题】:php response in ajaxajax中的php响应
【发布时间】:2016-08-09 16:23:30
【问题描述】:

我有一个 IP 范围 php 函数,如果 ip 在范围内,则返回 1。

PHP:

<?
$ip = $_SERVER['REMOTE_ADDR'];

$public_ip_ranges = array();

$range = (object) array();
$range->name = 'Barclays';
$range->lower = '141.228.0.0';
$range->upper = '141.228.255.255';
$public_ip_ranges[] = $range;

$range = (object) array();
$range->name = 'Incisive Media';
$range->lower = '10.1.0.0';
$range->upper = '10.1.255.255';
$public_ip_ranges[] = $range;

$range = (object) array();
$range->name = 'Barcap';
$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) {
       echo 1;
    }

}

?>

我有 ajax 引用这个文件,但我想做的是如果 PHP 函数 echo's 1 然后显示 x 文本,否则显示 Y 文本。

阿贾克斯:

   $.ajax( { 
            url: "/microsub.php",
            method: 'GET',
            success: function (data) {
             console.log(data);
             },
             error: function(error) {
                 console.log(error);
             }

        } );

所以我想从 php 获取响应(如果在范围内,它应该回显 1)并显示文本警报,否则如果不显示不同的文本警报

【问题讨论】:

  • 你的问题是......?这个网站是用来提问的,不是一个倾倒待办事项的地方。
  • 你可能想在你的success: function(data) 部分尝试这样的事情:if(data == 1){ // do something because the IP is in range } else { // do another thing because the IP is not in range }。就像 Marc 说的那样,我们需要您的代码来帮助您。
  • 你回显的任何内容都将存储在数据中
  • 首先,数据的输出是什么(通过ajax)?

标签: php jquery ajax


【解决方案1】:

你可以返回错误:

$isPublic = true;
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;

   $isPublic = ($lngIP >= $lngLow) && ($lngIP <= $lngHigh) && $isPublic;
}

if(!$isPublic) {
   header("HTTP/1.0 404 Not Found");
}

echo $isPublic;

【讨论】:

    猜你喜欢
    • 2022-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多