Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony2:app_dev.php是否只允许访问IP?_Php_Symfony - Fatal编程技术网

Symfony2:app_dev.php是否只允许访问IP?

Symfony2:app_dev.php是否只允许访问IP?,php,symfony,Php,Symfony,在我的Symfony2项目中,我希望app_dev.php只能通过我的IP地址访问。就像在config.php中一样,我可以设置一个IP数组,这样每个人都无法访问该文件。 这对于app_dev.php也是可能的吗?在app_dev.php中,您可以找到下面的代码 if (isset($_SERVER['HTTP_CLIENT_IP']) || isset($_SERVER['HTTP_X_FORWARDED_FOR']) || !in_array(@$_SERVER['REMOT

在我的Symfony2项目中,我希望app_dev.php只能通过我的IP地址访问。就像在config.php中一样,我可以设置一个IP数组,这样每个人都无法访问该文件。
这对于app_dev.php也是可能的吗?

在app_dev.php中,您可以找到下面的代码

if (isset($_SERVER['HTTP_CLIENT_IP'])
    || isset($_SERVER['HTTP_X_FORWARDED_FOR'])
    || !in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
您可以在此处设置要访问的IP地址

if (!in_array(@$_SERVER['REMOTE_ADDR'], array('Your IP address', '127.0.0.1', 'fe80::1', '::1'))
) {
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access this file. Check '.basename(__FILE__).' for more information.');
}
在虚拟主机中设置
/var/apache2/可用站点
ServerName domain.com/main
ServerAlias www.domain.com/main
DocumentRoot/var/www/domain/main/web
DirectoryIndex应用程序_dev.php
转换
ServerName domain.com/main
ServerAlias www.domain.com/main
DocumentRoot/var/www/domain/main/web
DirectoryIndex应用程序_dev.php

这是@chanchal118答案的一个细微变化。 我们的站点采用负载均衡器,因此IP的工作方式略有不同。 希望这将有助于类似设置的人

如果IP被欺骗,我也很想听听关于安全问题的想法

//todo this may be a security concern if someone managed to spoof their IP as one of these
$allowedIPs = array('127.0.0.1', 'fe80::1', '::1', 'my.organisation.ip.address');

//allow app_dev.php only under these conditions (prevent for production environment) uses HTTP_X_FORWARDED_FOR because behind load balancer
if (
    isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
    ( ! in_array(@$_SERVER['HTTP_X_FORWARDED_FOR'], $allowedIPs) )
){
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access the development environment.');
}

请注意,欺骗传入的ip地址是微不足道的。因此,虽然您可以仅使用给定的ip地址访问app_dev,但无法有效地将app_dev仅限于您的计算机。我怀疑,这才是你真正的目标。应该是&&!在数组中?这不是最佳实践。Insight(由Sensio分析)返回:“不鼓励使用PHP响应函数(如此处的header()),因为它会绕过Symfony事件系统。请改用HttpFoundationResponse类。”以及“$\u服务器超级全局不应使用。”请参阅我的回复@johnnyevolunium,这段代码直接来自symfony的官方应用程序_dev.phpp请解释您的答案如何解决问题,它将帮助每个人更清楚地理解您的解决方案,以供将来参考。
//todo this may be a security concern if someone managed to spoof their IP as one of these
$allowedIPs = array('127.0.0.1', 'fe80::1', '::1', 'my.organisation.ip.address');

//allow app_dev.php only under these conditions (prevent for production environment) uses HTTP_X_FORWARDED_FOR because behind load balancer
if (
    isset($_SERVER['HTTP_X_FORWARDED_FOR']) &&
    ( ! in_array(@$_SERVER['HTTP_X_FORWARDED_FOR'], $allowedIPs) )
){
    header('HTTP/1.0 403 Forbidden');
    exit('You are not allowed to access the development environment.');
}