How to create a text file with the client’s IP

Asked

Viewed 129 times

0

Good afternoon guys, I’m trying to create a text file with the client’s IP, so let’s say the server will create a file run X action,send the file to the client for download, then delete from the server. I’m just missing to know how to create the file with the client’s ip number, my intention is that if I set a default name for the file, and multiclientes access pag, when generating the file they do not conflict, maybe there is another way to do 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))



	
?>

  • 4

    Most people change their IP every day at the most varied times. Are you sure that’s what you want?

  • is temporary, meaning it doesn’t matter :D

2 answers

1


If you know the IP address you logged on to use something like 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;
    }
  • this I wanted to

  • only that instead of getenv I will use $_SERVER

0

It seems to have already had your answer, I use a function similar to that of Christian, very good, you can use the getenv that picks up for you by default. I wanted to take this opportunity to make 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 it has a lot of reading and writing can make it easier. It does the same thing, that those lines that reported.

  • yes, decrease 4 lines to 1 kkkkk

Browser other questions tagged

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