How to create a text file with client IP

0

Good afternoon guys, I'm trying to create a text file with the client IP, so let's say the server will create a file execute X action, send the file to download to the client, then deletes the server. I'm just missing out on how to create the file with the client's ip number, my intent is that if I set a default file name, and multi-paged access, when I generate the file they do not conflict, maybe I have another way of doing this ...

<?php			
	
	$arquivo = fopen("num_ip", "w");
	$texto = "Conteúdo do cliente aqui";
	fwrite($arquivo, $texto);
	fclose($arquivo);	
	
	function download( $path, $fileName = '' )
		{

			if( $fileName == '' )
				{
					$fileName = basename( $path );
				}

			header("Content-Type: application/force-download");
			header("Content-type: application/octet-stream;");
			header("Content-Length: " . filesize( $path ) );
			header("Content-disposition: attachment; filename=" . $fileName );
			header("Pragma: no-cache");
			header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
			header("Expires: 0");
			readfile( $path );
			flush();
    }
download( 'num_ip.txt', 'arquivo.txt' );

$arquivo = "num_ip.txt";
(!unlink($arquivo))



	
?>
    
asked by anonymous 10.12.2016 / 19:35

2 answers

1

If you want to know the ip that you have accessed, use this:

    function getIp() 
    {
        $ipaddress = '';
        if (getenv('HTTP_CLIENT_IP'))
            $ipaddress = getenv('HTTP_CLIENT_IP');
        else if(getenv('HTTP_X_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
        else if(getenv('HTTP_X_FORWARDED'))
            $ipaddress = getenv('HTTP_X_FORWARDED');
        else if(getenv('HTTP_FORWARDED_FOR'))
            $ipaddress = getenv('HTTP_FORWARDED_FOR');
        else if(getenv('HTTP_FORWARDED'))
           $ipaddress = getenv('HTTP_FORWARDED');
        else if(getenv('REMOTE_ADDR'))
            $ipaddress = getenv('REMOTE_ADDR');
        else
            $ipaddress = 'UNKNOWN';
        return $ipaddress;
    }
    
10.12.2016 / 19:44
0

It seems to have already had your answer, I use a function similar to that of Christian, very good, you can use getenv that takes you by default. I would like to take the time to give you a suggestion, where you have:

$arquivo = fopen("num_ip", "w");
$texto = "Conteúdo do cliente aqui";
fwrite($arquivo, $texto);
fclose($arquivo);   

You can do something like:

file_put_contents('num_ip', "Conteúdo do cliente aqui");

It's silly, but when you have a lot of reading and writing can make it easier. It does the same thing, as those lines you reported.

    
11.12.2016 / 07:26