rails重定向基于IP地址

时间:2017-03-16 14:50:50

标签: ruby-on-rails ruby-on-rails-5

随着app am的建立,我试图基于ipadress重定向用户,

<?php

$file = "online.mpg";

function flush_buffers(){
    ob_end_flush();
    ob_flush();
    flush();
    ob_start();
}

header('Content-Type: video/mpeg');
$stream = fopen( $file, "rb" );
fseek($stream, (filesize($file)-10000000), SEEK_SET);

while(1){
    $response = fread($stream, 8192); 
    echo $response;
    flush_buffers();
}

fclose( $stream );
exit();
?>

这是我试图将任何不是从冈比亚的IP地址重定向到订阅页面,目前这不起作用。

2 个答案:

答案 0 :(得分:1)

这个怎么样?

def check_country
  request_ip = IPAddr.new request.remote_ip # => Parse from text to IP Address
  unless gambian_blocks.any? { |block| block.include?(request_ip) }
    redirect_to subscribe_path
  end
end

修改

您也可以使用geoip gem。它支持IPV4&amp; IPV6。你可以这样做:

ip = GeoIP.new('GeoLiteCityv6.dat').city(request.remote_ip)
ip.contry_name # => 'Gambia'

答案 1 :(得分:0)

request.remote_ip返回字符串值,而gambian_blocks返回IPAddr个对象的数组。所以他们无法比较。因此,转换为所需结果的类似格式。

相关问题