简体   繁体   English

PHP中的主机名验证

[英]Host name validation in php

How to validate the host name in PHP? 如何在PHP中验证主机名?

That is if suppose am running my application server on IP:192.168.1.77 and if the request contains the Host name other than 192.168.1.77 it should not allow the further processing. 就是说,假设我的应用程序服务器在IP:192.168.1.77上运行,并且如果请求包含的主机名不是192.168.1.77则它不应允许进一步的处理。

Any help or suggestion would be appreciated. 任何帮助或建议,将不胜感激。

I'll throw a unauthorized page. 我将抛出未经授权的页面。 With error stating that there is mismatch in host name 错误提示主机名不匹配

What for? 做什么的? This is something the web server will already deal with if it's set up properly : 如果正确设置,这是Web服务器已经可以处理的事情:

If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host. 如果使用的是基于名称的虚拟主机,则部分中的ServerName指定必须在请求的Host:标头中显示的Host:名,以匹配该虚拟主机。

There's no need to additionally check for this in the PHP script. 无需在PHP脚本中另外进行检查。

It will be an additional security na? 这将是一个额外的安全性吗? Thats what my clients requirement is 那就是我的客户要求

It is entirely pointless and will not add any security whatsoever. 完全没有意义,不会增加任何安全性。 However, I guess there's no harm in doing it, either. 但是,我想这样做也没有害处。

I guess you mean address, not hostname. 我想你的意思是地址,而不是主机名。

if ($_SERVER['REMOTE_ADDR'] !== '192.168.1.77') die(header("Location: /")); 

if you really wan't to check the users hostname, use this 如果您确实不想检查用户的主机名,请使用此

if ($_SERVER['REMOTE_HOST'] !== '192.168.1.77') die(header("Location: /")); 

More info http://php.net/manual/en/reserved.variables.server.php 更多信息http://php.net/manual/zh/reserved.variables.server.php

I think this is something you will have to do server side from within your web server's configuration. 我认为这是您必须从Web服务器的配置中在服务器端执行的操作。 (.htaccess) (.htaccess)

order deny, allow
deny from all
allow from 192.168.1.77

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM