How to find out who requested a particular PHP file?

Asked

Viewed 1,072 times

0

I have a PHP file of a legacy system running perfectly on my server that checks this system and sends several emails with the alerts found. However, this file is executed through Cron Jobs, scheduled tasks, etc.

The problem is that I have no idea where this Cron Job is that is calling the file. But I know where the file is and can change it.

Is there any way to find out, through the file, who the client is doing this request?

  • 3

    It’s already spun one crontab -l on the server to see the list of scheduled tasks?

  • @bfavaretto the problem is right there. It’s not on the server where the application is. It’s some other server. As we have many virtual machines in our datacenter, we can not see one by one.

  • 1

    You said you have access to the script, right? Have the script save one print_r($_SERVER) in a log to find out who is calling you.

  • I did a little different. I added in the body of the email that is sent by the script another information: <?php echo $_SERVER['REMOTE_HOST']; ?>. Let’s see now if I now receive the email with the IP of the server that made the request.

1 answer

3


You can look in the apache access log file. Generally in linux it is in /var/log/apache2/access.:

$ grep nome_arquivo.php /var/log/apache2/access.log

Or put in your PHP file to display the IP address of the request source:

if (!empty($_SERVER["HTTP_CLIENT_IP"]))
{
 $ip = $_SERVER["HTTP_CLIENT_IP"];
}
elseif (!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
 $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
 $ip = $_SERVER["REMOTE_ADDR"];
}

echo $ip;
  • I had used only the REMOTE_ADDR, but complemented with your suggestion and managed to solve the problem. However, the echo to email out the file, since you will not see the Answer in the browser. Parsing the log I believe would also solve, but when I saw the size of the log on the server, I gave up for the size.

Browser other questions tagged

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