国家代码隐藏的div

I'm not a php-hero so, I try to hidden a section if the user not come from a specific country.

So I did this:

$.get("http://ipinfo.io", function (response) {
    $("#country").html(response.country);
}, "jsonp");

<input hidden id="country" type="text" name="country" value="">

This work well and show me the country code (eg. IT). Now I try to get this value and insert in a IF

$country = $_GET['country'];
$it = "IT";

<?php if ($country != $it): ?>
    Code to hidden here...
<?php endif; ?>

What is it wrong here?

Change

$("#country").html(response.country);

to

$("#country").val(response.country);

Because php $_GET saves values.

Also I do not see a reason to do this:

$it = "IT";
<?php if ($country != $it): ?>

You can just do

<?php if ($country != "IT"): ?>

And last but not least you should not access $_GET directly. It is better to use function filter_input which in your case would be filter_input(INPUT_GET, 'country')

EDIT

I do not understand what is the hidden input for. But if you want to show or hide content depending on the country, and you get the country using ajax there is absolutely no need for this input.

Instead of making php condition (<?php if ($country != "IT")...) You can do it in js. Let's say that inside your condition there is a div with class content

Solution

Your html would look more or less like this

<div class="content">
    <!-- Your content here -->
</div>

instead of php condition.

And in js you can do something like this

$.get("http://ipinfo.io", function (response) {
    if (response.country == "IT") {
        $(".content").hide();
    }
}, "jsonp");

So what do we do here?

We check if country code equals "IT". If it is true we hide the content. And this is the same what you were doing in php (if country different than IT show content).

EDIT 2

Instead of hiding the div you can remove it

$(".content").remove();

Try hiding via javascript, ajax can run PHP scripts and bring it back into the DOM but you're better off using JS if you don't need a backendscript

$.get("http://ipinfo.io", function (response) {
   var country = $("#country").html(response.country);
   if(country != "IT"){ document.getElementByID("country").display = "none";
}, "jsonp");

<input hidden id="country" type="text" name="country" value="">

I use the ProcessWise cms, that have their own API. So this answer work only with the ProcessWise cms. ( the best one ;) )

    <?PHP

function getUserIP()
{
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
    {
        $ip = $client;
    }
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
    {
        $ip = $forward;
    }
    else
    {
        $ip = $remote;
    }

    return $ip;
}


$user_ip = getUserIP();

echo "ip: " . $user_ip . "<br />";

$http = new WireHttp();
$url = "http://ipinfo.io/{$user_ip}/country";
$response = $http->get($url, ['country' => '']);


echo "Country: " . $response . "<br />";
echo "Successful response: " . $sanitizer->entities($response) . "<br />";

?>