首页 > 解决方案 > 根据使用 PHP 输入的用户所在国家/地区隐藏 div

问题描述

我得到了以这种方式进入的用户的ip地址。这些都没问题。

function getCountryData() {
    return unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=' . get_ip()));;
}

function getCountry() {
    $languages = [
        'TR' => 'tr_TR',
        'EN' => 'en_US'
    ];
    $response = getCountryData();

    return array_key_exists($response['geoplugin_countryCode'], $languages) ? $languages[$response['geoplugin_countryCode']] : 'en_US';
}

这是 HTML 端的正常部分。

<p class="d-none d-sm-inline-block"><span><a href="tel:000000">000000</a></span></p>

如果输入 TR 的用户正在输入,我想隐藏这部分。我在this中写了这样的东西,我哪里出错了或者我应该怎么做?

<p class="d-none d-sm-inline-block" style="<?php if ($response['geoplugin_countryCode']) {echo 'display: none!important;';} else {echo 'display: inline-block!important;';} ?>"><span><a href="tel:000000">000000</a></span></p>

标签: php

解决方案


您需要将 $response['geoplugin_countryCode'] 与 'TR' 之类的进行比较

 ... if ($response['geoplugin_countryCode']==='TR') {echo 'display: none!important;';} else...

推荐阅读