Help search ip

Asked

Viewed 43 times

0

I made a simple php, from Geolocation, but there is a however, I want it like this: when the guy accesses the site without searching ip no appears his default ip as it is already, but if he accesses and puts in the area of searching ip a certain ip and he clicks to search, he will search such ip, and I don’t know how to do this. If you can help me, I’d like to thank you.

Below is the code. I know the code has a lot of gambiarra but then fix, 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>

1 answer

0

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

<!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>
  • Thanks man, I had already managed to do with POST, but still, thanks for helping :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.