如何阻止/停用<a href=""> based on geoip data

时间:2017-02-23 10:15:40

标签: php geoip

I'm an absolute beginner when it comes to php and js and have been pulling my hair out trying to find a workable solution to the following requirement.

I have a Joomla subscription extension with a "subscribe" button using an a href tag.

What I'd like to achieve is, based on geoip location, visitors browsing from outside the UK are denied access to the button click action, but given a message explaining why.

So far all I'm able to achieve is a disabled button, which is still clickable...

The little-bit I've been able to achieve so far is below. Really hoping someone can assist me.

<?php
// Added Geo IP Location Tracking
$user_ip = getenv('REMOTE_ADDR');
// http://www.geoplugin.net/php.gp?ip=$user_ip
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip"));
$city = $geo["geoplugin_city"];
$region = $geo["geoplugin_regionName"];
$country = $geo["geoplugin_countryName"];
$countryCode = $geo["geoplugin_countryCode"];
}
?>


<?php
if ($countryCode == 'CH') {


echo "Unfortunately China is not covered by our service!"."<br>";           
}
?>

<p><a class="btn btn-large btn-primary" <?php if ($countryCode == 'CH'){ ?> disabled <?php   } ?> href="<?php echo JRoute::_('index.php?option=com_axisubs&view=subscribe&plan='.$item->slug); ?>">
<i class="icon-ok"></i>

<?php if ( $item->hasTrial() ) : ?>
<?php echo JText::_('COM_AXISUBS_START_TRIAL'); ?>
<?php else: ?>
<?php echo JText::_('COM_AXISUBS_SUBSCRIBE_NOW'); ?>
<?php endif; ?>
</a>

2 个答案:

答案 0 :(得分:1)

您无法将链接设置为“已禁用”,这仅适用于输入,文本字段,按钮等表单元素。

您可以做的是添加一些javascript以防止链接的默认操作:

<a class="btn btn-large btn-primary" <?= ($countryCode != 'CH')?'onclick="return false;"':'' ?> href="<?php echo JRoute::_('index.php?option=com_axisubs&view=subscribe&plan='.$item->slug); ?>">

通过使用此方法,您还可以在警告框中添加消息,例如当用户不在所需国家/地区时:

<a class="btn btn-large btn-primary" <?= ($countryCode != 'CH')?'onclick="alert(\'you are not in CH\');return false;"':'' ?> href="<?php echo JRoute::_('index.php?option=com_axisubs&view=subscribe&plan='.$item->slug); ?>">

如果您必须阻止许多国家/地区代码,则可以执行此操作:

<a class="..." <?= (in_array($countryCode, ['CH', 'FR', 'UK', 'DE']))?'onclick="alert(\'you are not in an allowed country\');return false;"':'' ?> href="...">

答案 1 :(得分:1)

您不应该包含您的链接,以防止滥用您的服务。

<a class="btn btn-large btn-primary" <?php echo $link ?>>

然后输出您的链接标记

{{1}}

这样,您的页面上不存在链接,虚拟链接无法点击。