Help search ip

0

I made a simple php, geolocation, but it has a however, I want it: when the guy accesses the site without searching ip none appears the default ip as it is already, but if he accesses and boats in the area of ip search certain ip and it click on search, it will search such ip, and do not know how to do this. If you can help me, thank you.

Follow the code below. I know the code has a lot of gambiarra but after I tidy, the important thing is to be functional.

<title>PEGADOR DE IP</title>
<head>
<style>
body{
  background-color: #34495e;
}   

body {
  color: fff;
}

.startcheck{
  background-color: #55C34D;
  border: none;
  resize: none;
  outline: none;
  width: 250px;
  height: 25px;
  color: white;
}

.listcc{
  width: 300px;
  height: 40px;
  background-color: #353C3E;
  border: none;
  outline: none;
  resize: none;
  color: white;
  text-align: center;
}

#dados {
  width: 400px;
  margin: 0 auto;
  border: 1px solid #000;
  text-align: center;
}

#dados ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

#dados ul li {
  padding: 5px 5px;
}

#dados p {
  text-align: center;
}
</style>
</head>
<br>
<center><input type="text" class="listcc" class="text" placeholder="PESQUISAR IP"></center>
<br>
<center><input type="submit" value="PESQUISAR" class="startcheck"></center><br><br><br>

<?php
header("Content-type: text/html; charset=utf-8"); //Seta o padrão como utf-8
error_reporting(0); //Bloqueia o sistema de mostrar algum erro inutil
//Inicio Vars
$user_ip = getenv('REMOTE_ADDR'); //Vai Puxar o ip do usuario
$geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip")); //Entra no site e puxa os dados
$city = $geo["geoplugin_city"]; //Vai puxar a cidade
$region = $geo["geoplugin_regionName"]; //Vai puxar o estado
$country = $geo["geoplugin_countryName"]; //Vai puxar o país
$browser =$_SERVER['HTTP_USER_AGENT']; //Vai puxar o navegador
date_default_timezone_set('America/Sao_Paulo'); //Vai definir a hora padrão de São paulo - America
$data =date("Y-m-d"); //Vai puxar a data
$hora =date("H:i:s"); //Vai puxar a hora
$useragent = $_SERVER['HTTP_USER_AGENT']; //Vai puxar o navegador utilizado
if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
   $browser_version=$matched[1];
   $browser = 'IE';
} elseif (preg_match( '|Opera/([0-9].[0-9]{1,2})|',$useragent,$matched)) {
   $browser_version=$matched[1];
   $browser = 'Opera';
} elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
   $browser_version=$matched[1];
   $browser = 'Firefox';
} elseif(preg_match('|Chrome/([0-9\.]+)|',$useragent,$matched)) {
   $browser_version=$matched[1];
   $browser = 'Chrome';
} elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
   $browser_version=$matched[1];
   $browser = 'Safari';
} else {
   // browser not recognized!
   $browser_version = 0;
   $browser= 'other';
}
//Fim Vars
?>

<div id="dados">
   <ul>
  <li>IP: <?php echo $user_ip; ?></li>
  <li>CIDADE: <?php echo $city; ?></li>
  <li>ESTADO: <?php echo $region; ?></li>
  <li>PAÍS: <?php echo $country; ?></li>
  <li>NAVEGADOR: <?php echo $browser; ?></li>
  <li>DATA: <?php echo $data; ?></li>
  <li>HORA: <?php echo $hora; ?></li>
    
asked by anonymous 05.11.2017 / 03:52

1 answer

0

You need to enter the input where you type the ip inside a form tag and then receive the value sent by that form in your php code. The code with the changes (the changes are commented):

<!DOCTYPE html>
<html>
<head>
<title>PEGADOR DE IP</title>
    <style>
        body{
            background-color: #34495e;
        }   
        body {
        color: fff;
        }
        .startcheck{
            background-color: #55C34D;
            border: none;
            resize: none;
            outline: none;
            width: 250px;
            height: 25px;
            color: white;
        }
        .listcc{
            width: 300px;
            height: 40px;
            background-color: #353C3E;
            border: none;
            outline: none;
            resize: none;
            color: white;
            text-align: center;
        }
        #dados {
        width: 400px;
        margin: 0 auto;
        border: 1px solid #000;
        text-align: center;
        }
        #dados ul {
        margin: 0;
        padding: 0;
        list-style: none;
        }
        #dados ul li {
        padding: 5px 5px;
        }
        #dados p {
        text-align: center;
        }
    </style>
</head>

<body>
    <br>
    <!--Formulario para enviar o ip digitado pelo usuario
    Nesse caso como não defino uma action na tag forma, a mesma pagina
    será chamada ao submeter o formulario. Trato isso no PHP -->
    <form method="post">
        <center>
        <!--Colocar um name para o input-->
        <input type="text" name="pesquisar_ip" class="listcc" class="text" placeholder="PESQUISAR IP">
        </center>
        <br>
        <center>
        <input type="submit" value="PESQUISAR" class="startcheck">
        </center>
    </form>
    <br><br><br>

<?php
    header("Content-type: text/html; charset=utf-8"); //Seta o padrão como utf-8
    error_reporting(0); //Bloqueia o sistema de mostrar algum erro inutil

    /*
    Verifica se o formulario foi submetido, se sim obtem o ip
    */
    $pesquisar_ip = null;
    if(isset($_POST['pesquisar_ip'])){
        $pesquisar_ip = $_POST['pesquisar_ip'];
    }else{
        $pesquisar_ip = getenv('REMOTE_ADDR'); //Vai Puxar o ip do usuario
    }
    //Inicio Vars
    $user_ip = $pesquisar_ip;
    $geo = unserialize(file_get_contents("http://www.geoplugin.net/php.gp?ip=$user_ip")); //Entra no site e puxa os dados
    $city = $geo["geoplugin_city"]; //Vai puxar a cidade
    $region = $geo["geoplugin_regionName"]; //Vai puxar o estado
    $country = $geo["geoplugin_countryName"]; //Vai puxar o país
    $browser =$_SERVER['HTTP_USER_AGENT']; //Vai puxar o navegador
    date_default_timezone_set('America/Sao_Paulo'); //Vai definir a hora padrão de São paulo - America
    $data =date("Y-m-d"); //Vai puxar a data
    $hora =date("H:i:s"); //Vai puxar a hora
    $useragent = $_SERVER['HTTP_USER_AGENT']; //Vai puxar o navegador utilizado
    if (preg_match('|MSIE ([0-9].[0-9]{1,2})|',$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = 'IE';
    } elseif (preg_match( '|Opera/([0-9].[0-9]{1,2})|',$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = 'Opera';
    } elseif(preg_match('|Firefox/([0-9\.]+)|',$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = 'Firefox';
    } elseif(preg_match('|Chrome/([0-9\.]+)|',$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = 'Chrome';
    } elseif(preg_match('|Safari/([0-9\.]+)|',$useragent,$matched)) {
        $browser_version=$matched[1];
        $browser = 'Safari';
    } else {
    // browser not recognized!
    $browser_version = 0;
    $browser= 'other';
    }
    //Fim Vars

?>

    <div id="dados">
    <ul>
    <li>IP: <?php echo $user_ip; ?></li>
    <li>CIDADE: <?php echo $city; ?></li>
    <li>ESTADO: <?php echo $region; ?></li>
    <li>PAÍS: <?php echo $country; ?></li>
    <li>NAVEGADOR: <?php echo $browser; ?></li>
    <li>DATA: <?php echo $data; ?></li>
    <li>HORA: <?php echo $hora; ?></li>
</body>
</html>
    
05.11.2017 / 04:37