Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

PHP How to make an ip logger on a direct download link?

I have such an iplogger with a report in a telegram, taken from here githab . The code itself:

PHP:
<?php

function telegram($msg) {
        global $telegrambot,$telegramchatid;
        $url = 'https://api.telegram.org/bot'.$telegrambot.'/sendMessage';$data = array('chat_id'=>$telegramchatid,'text'=>$msg);
        $options = array('http'=>array('method' => 'POST','header' => "Content-Type:application/x-www-form-urlencoded\r\n",'content' => http_build_query($data),),);
        $context = stream_context_create($options);
        $result = file_get_contents($url,false,$context);
        return $result;
}

$telegrambot = '1318525370:AAEXFi11J4LGARh62fqnj0vD9QkN1r8946s'; // enter bot token
$telegramchatid = 1085665214; // enter chat id

$userAgent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$ipapi = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));
$datetime = date("g:ia, l F j Y"); // g:ia l F j Y   l, F j, Y, g:ia

telegram("New user:
        IP  :  $ip      
        Browser  :  $userAgent
        Country  :  $ipapi->country ($ipapi->countryCode)
        Region  :  $ipapi->regionName ($ipapi->region)
        City  :  $ipapi->city
        Zip (Postcode)  :  $ipapi->zip
        Time  :  $datetime
        Internet Provider  :  $ipapi->isp ($ipapi->org)
           
        ");

// Operating system $user_os
// Browser $user_browser
?>

<?php

<source>

?>
Is it possible to change the code so that it works for a specific download link? I create a direct download link to use in different articles, so that users can download files without going to the site itself. But in this case, the ip logger doesn't work and I can't get the data.

I just met with programming and don't understand, please show me step by step if possible.

ip logger is used to collect information and then analyze the effectiveness of advertising aimed at specific cities.
 
Solution
Putting it all together and simplifying it a bit:

[CODE lang="php" title="Get user IP and send to Telegram"]<?php

// Get User IP and details
$ip = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Get geolocation data about IP
$ipapi = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));

// Your Telegram Credentials

$telegrambot = '1318525370:AAEXFi11J4LGARh62fqnj0vD9QkN1r8946s'; // enter bot token
$telegramchatid = 1085665214; // enter chat id

// Message to send to Telegram
$msg = "New user with IP address " . $ip . " downloaded file. The user's country is " . $ipapi->country ($ipapi->countryCode) . " and their browser user agent is " . $userAgent . "!";

// Send IP to Telegram
$url = 'https://api.telegram.org/bot'.$telegrambot.'/sendMessage'...
There are more efficient ways of storing IP addresses for what you seem to be doing (storing geo-location data) than having them sent one by one to Telegram. For instance, you could store the IPs and files downloaded in a database so you could create a report that lists files by location.

The problem with direct downloads is, they will bypass PHP. So what you need to do is have an intermediary process handle actually fetching the file that the user downloads. Instead of yoursite.com/file.pdf you need something like yoursite.com/download.php?file=file.pdf.

Take a look at the fread() example here:

You would want to make a file that:

1) Determines which file the user is attempting to download
2) Checks the file system to ensure the file exists
3) Logs the user's IP address using the code you have above
4) Uses fread() to download the file to the user's browser.
 
If you have multiple files you will need to do all the items to allow a choice of files. If you only have a single file users will ever download you could get away with only doing 2,3 and 4.

I would recommend looking at the documentation I posted and trying a few things. If you get stuck you can post your code here and I'll take a look.
 
If you have multiple files you will need to do all the items to allow a choice of files. If you only have a single file users will ever download you could get away with only doing 2,3 and 4.

I would recommend looking at the documentation I posted and trying a few things. If you get stuck you can post your code here and I'll take a look.
I only have one file and I need 3 and 4. I have read the documentation you provided. But it didn't help me. I used the fread() function, and it displays information from text files on the screen. But I have an exe file and it doesn't work for downloading.

In addition, I do not understand how I can make an intermediate process in order for the third point to work. I created download.php and I put the code from the example with the fread() function there. Added a text file for code health and used the following link yoursite.com/download.php. But I didn't get the ip report.

Eventually, in download.php I put the code to force the file upload:

PHP:
$file_url = 'http://www.mysite.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);

But how do I do 3 and 4 I still don't understand. 🙄
 
Actually, fread() is the wrong function for outputting the file, sorry about that. The correct function to use is readfile() which you are using in the latest code you posted. It's been a while since I've had to work with outputting a file directly like you are doing. What you have so far looks correct, so all you need to do is log the user's IP address.

You can log the IP using $_SERVER['REMOTE_ADDR'] which will get you the user's IP, assuming that your site is not behind a proxy like Cloudflare.

Where do you want to store the IPs? I think it would be best to have a database or a file that has the user's IP and time/date along with maybe some other information, like referrer, browser, etc, depending on what you want to track.
 
Actually, fread() is the wrong function for outputting the file, sorry about that. The correct function to use is readfile() which you are using in the latest code you posted. It's been a while since I've had to work with outputting a file directly like you are doing. What you have so far looks correct, so all you need to do is log the user's IP address.

You can log the IP using $_SERVER['REMOTE_ADDR'] which will get you the user's IP, assuming that your site is not behind a proxy like Cloudflare.

Where do you want to store the IPs? I think it would be best to have a database or a file that has the user's IP and time/date along with maybe some other information, like referrer, browser, etc, depending on what you want to track.
I want to store the ip information in the telegram, since there is already a system for storing and organizing the received information. Accordingly, I need to use the code from the first message to get the ip.
More precisely, I need to get all the information that is specified in the first code, and send it to telegram.
 
Putting it all together and simplifying it a bit:

[CODE lang="php" title="Get user IP and send to Telegram"]<?php

// Get User IP and details
$ip = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Get geolocation data about IP
$ipapi = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));

// Your Telegram Credentials

$telegrambot = '1318525370:AAEXFi11J4LGARh62fqnj0vD9QkN1r8946s'; // enter bot token
$telegramchatid = 1085665214; // enter chat id

// Message to send to Telegram
$msg = "New user with IP address " . $ip . " downloaded file. The user's country is " . $ipapi->country ($ipapi->countryCode) . " and their browser user agent is " . $userAgent . "!";

// Send IP to Telegram
$url = 'https://api.telegram.org/bot'.$telegrambot.'/sendMessage';
$data = array('chat_id'=>$telegramchatid,'text'=> $msg);

$options = array('http'=>
array('method' => 'POST','header' => "Content-Type:application/x-www-form-urlencoded\r\n",'content' => http_build_query($data),
),
);

$context = stream_context_create($options);
$result = file_get_contents($url,false,$context);

// Force the file download
$file_url = 'http://www.mysite.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);[/CODE]

Since you only call the Telegram code once, no need for a function. I haven't tested this, but it should be pretty close to what you need.
 
Putting it all together and simplifying it a bit:

[CODE lang="php" title="Get user IP and send to Telegram"]<?php

// Get User IP and details
$ip = $_SERVER['REMOTE_ADDR'];
$userAgent = $_SERVER['HTTP_USER_AGENT'];

// Get geolocation data about IP
$ipapi = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));

// Your Telegram Credentials

$telegrambot = '1318525370:AAEXFi11J4LGARh62fqnj0vD9QkN1r8946s'; // enter bot token
$telegramchatid = 1085665214; // enter chat id

// Message to send to Telegram
$msg = "New user with IP address " . $ip . " downloaded file. The user's country is " . $ipapi->country ($ipapi->countryCode) . " and their browser user agent is " . $userAgent . "!";

// Send IP to Telegram
$url = 'https://api.telegram.org/bot'.$telegrambot.'/sendMessage';
$data = array('chat_id'=>$telegramchatid,'text'=> $msg);

$options = array('http'=>
array('method' => 'POST','header' => "Content-Type:application/x-www-form-urlencoded\r\n",'content' => http_build_query($data),
),
);

$context = stream_context_create($options);
$result = file_get_contents($url,false,$context);

// Force the file download
$file_url = 'http://www.mysite.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);[/CODE]

Since you only call the Telegram code once, no need for a function. I haven't tested this, but it should be pretty close to what you need.
I used the code you offered me. But it turned out to be inoperable for two reasons:
1. The download did not start and the browser referred to an error in line 16 of the code.
2. ip logger did not work.
The error in line 16 of the code was somehow related to this section of the $ipapi->country ($ipapi->countryCode), but I decided not to go into it.

After looking at your code again, I understood how to connect ip logging and file uploading. In the end, it turned out this way:

PHP:
<?php

function telegram($msg) {
        global $telegrambot,$telegramchatid;
        $url = 'https://api.telegram.org/bot'.$telegrambot.'/sendMessage';$data = array('chat_id'=>$telegramchatid,'text'=>$msg);
        $options = array('http'=>array('method' => 'POST','header' => "Content-Type:application/x-www-form-urlencoded\r\n",'content' => http_build_query($data),),);
        $context = stream_context_create($options);
        $result = file_get_contents($url,false,$context);
        return $result;
}

$telegrambot = 'xxx'; // enter bot token
$telegramchatid = xxx; // enter chat id

$userAgent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$ipapi = json_decode(file_get_contents("http://ip-api.com/json/{$ip}"));
$datetime = date("g:ia, l F j Y"); // g:ia l F j Y   l, F j, Y, g:ia

telegram("New user:
        IP  :  $ip
        Browser: $userAgent
        Country  :  $ipapi->country ($ipapi->countryCode)
        Region  :  $ipapi->regionName ($ipapi->region)
        City  :  $ipapi->city
        Zip (Postcode)  :  $ipapi->zip
        Time  :  $datetime
        Internet Provider  :  $ipapi->isp ($ipapi->org)
    
        
        ");


// Force the file download
$file_url = 'https://xxx';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
And everything worked the way I wanted it to.

I am grateful to you for your help and the time you have spent on me. You are the only one who decided to help me deal with such simple things.

Thank you.
 
Solution

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom