bash 如何获取有关从服务器请求页面的客户端的信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12560544/
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
How to get information about the client that requests page from Server?
提问by Mitul Patel
I need to get information about the client that requests a page from the server. I need to get information about client IP and which page is request? I would prefer a console application i.e. Shell or Bash script in linux[Ubuntu]. If the page is requested there would be instant print message to the console or terminal. In that message it should be included the IP address and page requested. I also wanted to save the IP address and the requested page into the database through MySQL. I'd appreciate your answer and solution.
我需要获取有关从服务器请求页面的客户端的信息。我需要获取有关客户端 IP 以及请求哪个页面的信息?我更喜欢 linux[Ubuntu] 中的控制台应用程序,即 Shell 或 Bash 脚本。如果页面被请求,将会有即时打印消息发送到控制台或终端。在该消息中,应包含请求的 IP 地址和页面。我还想通过 MySQL 将 IP 地址和请求的页面保存到数据库中。我很感激你的回答和解决方案。
Thanks
谢谢
采纳答案by Mitul Patel
Hey I got my problem solved. Just got clue from @air4X
嘿,我的问题解决了。刚刚从@air4X 得到线索
watch -n -1 'd=`date +"%d/%b/%Y:%T"`;tail -n 20 /opt/lampp/logs/access_log '
Thanks for great help....
感谢您的大力帮助....
回答by air4x
try using watch+grep+dateon the webserver access log. In my case it is
尝试使用watch+ grep+ dateweb服务器上的访问日志。在我的情况下是
watch -n -1 'd=`date +"%d/%b/%Y:%T"`;grep $d /pgms/nginx1117/logs/access.log '
You would have to watch out for the date format in the access log. Also the output vanishes within a second.
您必须注意访问日志中的日期格式。输出也会在一秒钟内消失。
[Edit: use the below to extend the display to 3 seconds. again you will have to customize it for your configuration.]
[编辑:使用以下内容将显示延长至 3 秒。再次,您将不得不根据您的配置对其进行自定义。]
watch -n 1 '
d0=`date +"%S"`;
if [ $d0 -ne 0 ]; then d1=$(printf %02d `expr $d0 - 1`); else d1=59; fi ;
if [ $d1 -ne 0 ]; then d2=$(printf %02d `expr $d1 - 1`); else d2=59; fi ;
d=`date +"%d/%b/%Y:%H:%M:"`;
grep -e "$d$d0" -e "$d$d1" -e "$d$d2" /pgms/nginx1117/logs/access.log | awk "{ print $1,$7 }"
'
回答by Technolust
Run this small PHP Script:
运行这个小的 PHP 脚本:
<?php
foreach($_SERVER as $key => $value){
echo '$_SERVER["'.$key.'"] = '.$value."<br />";
}
?>
回答by Yogesh Suthar
You have to use $_SERVER[]in php.
你必须$_SERVER[]在php中使用。
This will give you all information of client and server you needed.
这将为您提供所需的客户端和服务器的所有信息。
回答by aravind3
For all the server variable use You have to use $_SERVER[] in php.
@Yogesh Suthar is absolutely correct and being specific you can use these variables.
For IP address
$_SERVER['REMOTE_ADDR'];
For Request Page
$_SERVER["HTTP_REFERER"];

