【问题标题】:How to get and save a client's IP address, location, and other data In PHP?如何在 PHP 中获取和保存客户端的 IP 地址、位置和其他数据?
【发布时间】:2020-08-17 08:14:48
【问题描述】:

我正在使用此代码来获取客户的信息。此代码正在运行,但它给出了一些错误。请检查我的代码,并帮助我更正它。此外:如何将用户数据存储到 MySQL 中?我enter image description here 将非常感谢你。错误如下。 注意:未定义索引:HTTP_REFERER 注意:未定义属性:stdClass::$loc in 注意:未定义属性:stdClass::$org in 注意:未定义属性:stdClass::$city in 注意:未定义属性:stdClass::$region in注意:未定义的属性:stdClass::$country 在注意:未定义的索引:HTTP_REFERER 在注意:未定义的属性:stdClass::$loc 在注意:未定义的属性:stdClass::$org 在注意:未定义的属性:stdClass::$city在注意:未定义的属性:stdClass::$region 在注意:未定义的属性:stdClass::$country 在

<?php
  // This program generates a web pages that gets 
  // the user's information, saves it to a file, 
  // and displays it on the web page.
  // Created by Mitchell Robinson.
  // 27 July, 2014.
  
  // Name of the ip address log.
  $outputWebBug = 'iplog.csv';

  // Get the ip address and info about client.
  @ $details = json_decode(file_get_contents("http://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/json"));
  @ $hostname=gethostbyaddr($_SERVER['REMOTE_ADDR']);
  
  // Get the query string from the URL.
  $QUERY_STRING = preg_replace("%[^/a-zA-Z0-9@,_=]%", '', $_SERVER['QUERY_STRING']);
  
  // Write the ip address and info to file.
  @ $fileHandle = fopen($outputWebBug, "a");
  if ($fileHandle)
  {
    $string ='"'.$QUERY_STRING.'","' // everything after "?" in the URL
      .$_SERVER['REMOTE_ADDR'].'","' // ip address
      .$hostname.'","' // hostname
      .$_SERVER['HTTP_USER_AGENT'].'","' // browser and operating system
      .$_SERVER['HTTP_REFERER'].'","' // where they got the link for this page
      .$details->loc.'","' // latitude, longitude
      .$details->org.'","' // internet service provider
      .$details->city.'","'  // city
      .$details->region.'","' // state
      .$details->country.'","' // country
      .date("D dS M,Y h:i a").'"' // date
      ."\n"
      ;
     $write = fputs($fileHandle, $string);
    @ fclose($fileHandle);
  }

  $string = '<code>'
    .'<p>'.$QUERY_STRING.'</p><p>IP address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    .$_SERVER['REMOTE_ADDR'].'</p><p>Hostname:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    .$hostname.'</p><p>Browser and OS:&nbsp;'
    .$_SERVER['HTTP_USER_AGENT'].'</p><p>'
    .$_SERVER['HTTP_REFERER'].'</p><p>Coordinates:&nbsp;&nbsp;&nbsp;&nbsp;'
    .$details->loc.'</p><p>ISP provider:&nbsp;&nbsp;&nbsp;'
    .$details->org.'</p><p>City:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    .$details->city.'</p><p>State:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    .$details->region.'</p><p>Country:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    .$details->country.'</p><p>Date:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    .date("D dS M,Y h:i a").'</p></code>'
    ;

  echo '<!DOCTYPE html><html><head><title>Who Am I?</title></head><body>';
  echo $string;
  echo '</body></html>';
?>

【问题讨论】:

  • 但是给出了一些错误,给你什么错误?,告诉我们。
  • 请粘贴错误
  • @GNassro 我已经附上了图片错误请检查..
  • 我建议你删除@符号,检查是否有其他错误
  • 请编辑您的问题并添加说明。不要在 cmets 中提供。

标签: php phpmyadmin


【解决方案1】:

把它放在你的 .htaccess 文件中

php_flag display_errors 关闭

【讨论】:

    猜你喜欢
    • 2021-10-22
    • 2019-07-12
    • 2013-07-07
    相关资源
    最近更新 更多