Javascript 记录用户IP地址、日期和时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6837655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:23:42  来源:igfitidea点击:

Log user ip address, date and time

phpjavascriptshtml

提问by gravityboy

Is there a simple script or piece of code I can add to my page to keep a log of every visitor, the date and time they hit the page and IP address? And what would be the best way to do this... javascript, php, something else?

是否有一个简单的脚本或一段代码可以添加到我的页面来记录每个访问者、他们访问页面的日期和时间以及 IP 地址?什么是最好的方法来做到这一点... javascript、php 或其他?

EDIT:

编辑:

Ouch...

哎哟...

Here is what happened... When I went to my server with FileZilla there were all the domain names (about 20) I have being logged like my domain.com so I found the one I needed and checked the logs but it was mainly search engines.

这是发生的事情......当我使用 FileZilla 去我的服务器时,所有的域名(大约 20 个)都像我的 domain.com 一样被记录,所以我找到了我需要的一个并检查了日志,但主要是搜索引擎。

But I just went back and happened to scroll down to stuff that was out of view and there were all the domain names again with www in front like www.mydomain.com and of course the logs in there are huge and have every single bit of info I need.

但我刚回去,碰巧向下滚动到看不见的东西,所有的域名再次出现在前面,如 www.mydomain.com,当然,那里的日志很大,并且有我需要的信息。

This happened because I found what I was looking for mydomain.com and of course I stopped looking. I didn't know or see there was a whole other set out of view... honest mistake.

发生这种情况是因为我找到了我在寻找 mydomain.com 的内容,当然我不再寻找了。我不知道或看到还有一个完全不同的场景......诚实的错误。

I am still using that code because it is nice and small, the logs are freakin' huge and take hours to download and look at.

我仍在使用该代码,因为它既美观又小巧,日志非常庞大,需要数小时才能下载和查看。

回答by deceze

$line = date('Y-m-d H:i:s') . " - $_SERVER[REMOTE_ADDR]";
file_put_contents('visitors.log', $line . PHP_EOL, FILE_APPEND);

Consider also logging $_SERVER['REQUEST_URI']or other interesting information, possibly in a more standard format as outlined by @Day.

还要考虑日志记录$_SERVER['REQUEST_URI']或其他有趣的信息,可能采用@Day 概述的更标准的格式。

回答by Daniel

<?php
    // include this piece of code in every page call

    // write in database row
    $log = array('time' => time(), 'ip' => $_SERVER['REMOTE_ADDR'], 'url' => $_SERVER['REQUEST_URI']);
?>

回答by Day

The simplest piece of code to add to your page is no code at all. So might I suggest "something else"? Try using your webserver's built-in request logging facility instead of writing some custom PHP code.

添加到页面的最简单的一段代码就是根本没有代码。那么我可以建议“别的东西”吗?尝试使用您的网络服务器的内置请求日志记录工具,而不是编写一些自定义的 PHP 代码。

Apache and many other webservers can produce logs in the Common Log Format(CLF) and there are many tools available to analyse such logs and draw pretty graphs for you (Webalizer, Awstatsetc). A CLF log line looks like this which gives you all the information you asked for and more:

Apache 和许多其他网络服务器可以生成通用日志格式(CLF) 的日志,并且有许多工具可用于分析此类日志并为您绘制漂亮的图表(WebalizerAwstats等)。CLF 日志行看起来像这样,它为您提供了您要求的所有信息以及更多信息:

127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326 

See the appropriate bit of documentation for your webserver for how to configure logging and give it a whirl:

请参阅您的网络服务器的相应文档,了解如何配置日志记录并试一试:

回答by evilbudz

here is my little script to log ip addresses dont forget to add the below after the /HEAD tag also note to make this work it must be a PHP not HTML

这是我记录 ip 地址的小脚本 不要忘记在 /HEAD 标签后添加以下内容 还要注意要使其工作它必须是 PHP 而不是 HTML

<?php include ('log-ip.php') ?>

where ever you want it called from

你想从哪里调用它

"log-ip.php"

“日志ip.php”

<?php
$iplogfile = 'logs/ip-address-mainsite.html';
$ipaddress = $_SERVER['REMOTE_ADDR'];
$webpage = $_SERVER['SCRIPT_NAME'];
$timestamp = date('d/m/Y h:i:s');
$browser = $_SERVER['HTTP_USER_AGENT'];
$fp = fopen($iplogfile, 'a+');
chmod($iplogfile, 0777);
fwrite($fp, '['.$timestamp.']: '.$ipaddress.' '.$webpage.' '.$browser. "\n<br><br>");
fclose($fp);
?>

and the resault is a nice web HTML log file logs/ip-address-mainsite.html

结果是一个不错的网页 HTML 日志文件 logs/ip-address-mainsite.html

<!DOCTYPE html><!-- HTML5 -->

<head>
<body bgcolor="#000000">
<title>NZ Quakes - Main Web Site Log</title>

</head>

<body>
<font color="#7FFF00">
<center>NZ Quakes - Main Web Site Log</center>
<font color="gold">
<br><center>
[01/04/2017 08:25:21]: 124.197.9.181 /index.php Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36
<br><br>

below is a picture of what it looks like.

下面是它的样子的图片。

enter image description here

在此处输入图片说明

what do you think about this i think its clean and simple sort of.

你怎么看这个我认为它干净简单。

回答by Dor

Most comprehensive - Apache's access log: Log Files -> Access Log @ httpd.apache.org

最全面 - Apache 的访问日志:Log Files -> Access Log @ httpd.apache.org