我有一个网站,我想跟踪访问该网站的所有用户代理。并将所有用户代理保存在文件中,或通过电子邮件发送。我如何在 PHP 或 JS 中实现这一点?
最佳答案
好的,需要发生三件事。写入文件,邮寄内容,邮寄后清除文件。第一部分将在一个单独的文件中,而邮件和文件清除将在另一个文件中
<?php
// get the user agent
$user_agent = $_SERVER['HTTP_USER_AGENT'];
// write to file
file_put_contents( 'user_agents.log', $user_agent, FILE_APPEND );
?>
<?php
// fetch the list of user agents from the file
$body = file_get_contents( 'user_agents.log' );
// mail to whereva
mail( 'admin@example.com', 'User Agent Log', $body );
// truncate the file back to zero
$fh = fopen( 'user_agents.log', 'w' );
fclose($fh);
?>
关于javascript - 跟踪访问站点的用户代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334058/