首页 文章

PHP - 通过htaccess进行页面调用时获取IP

提问于
浏览
1

我有一个PHP网站,需要知道访问客户端的IP是什么 . 为此,我使用以下功能:

function get_client_ip() {
    $ipaddress = '';
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    }

    return $ipaddress;
}

但是当通过url omenorpreco.com/teste访问该站点时,该页面将返回服务器IP .

当访问url omenorpreco.com/teste.php页面时,该页面返回客户端IP .

可能会发生此错误,因为当您访问没有扩展名“.php”的页面时,服务器会通过.htaccess解释页面?

如何调整我的应用程序进行访问,返回客户端的IP,而不是服务器IP?

在我的htaccess代码之上

php_value allow_url_fopen on
    php_value allow_url_include 1
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !.(js|ico|txt|gif|bmp|jpeg|jpg|png|css|log|rss|zip|xml|sql|pdf|doc|docx|xls)$ url_amigavel.php
    RewriteRule sitemap-categoria.xml$ sitemap.php?number=categoria
    RewriteRule sitemap-([0-9]+).xml$ sitemap.php?number=$1
    RewriteRule sitemap_index.xml$ sitemap_index.php

和url_amigavel.php代码

<?php

$geturl = explode( "/", str_replace( strrchr( $_SERVER["REQUEST_URI"], "?" ), "", $_SERVER["REQUEST_URI"] ) );
array_shift( $geturl );

$tipo = $geturl[0];


if ( is_file( "$tipo.php" ) )
{
    include "$tipo.php";
}
else
{
    echo "page not found";

}

?>

编辑:我可以使用客户端IP设置GLOBAL_VAR吗?

3 回答

  • 1

    我想我刚看到它在您的服务器上运行!改变 .htaccess 到;

    php_value allow_url_fopen on
    php_value allow_url_include 1
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !.(js|ico|txt|gif|bmp|jpeg|jpg|png|css|log|rss|zip|xml|sql|pdf|doc|docx|xls)$ url_amigavel.php
    RewriteRule sitemap-categoria.xml$ sitemap.php?number=categoria
    RewriteRule sitemap-([0-9]+).xml$ sitemap.php?number=$1
    RewriteRule sitemap_index.xml$ sitemap_index.php
    
    SetEnvIfNoCase Remote_Host "(.*)" HTTP_MY_REMOTE_HOST=$1
    SetEnvIfNoCase Remote_Addr "(.*)" HTTP_MY_REMOTE_ADDR=$1
    

    改变 test.php 到;

    function get_client_ip() {
        if (isset($_SERVER[REDIRECT_HTTP_MY_REMOTE_ADDR])) {
            return $_SERVER[REDIRECT_HTTP_MY_REMOTE_ADDR];
    
        } else if (isset($_SERVER[REDIRECT_HTTP_MY_REMOTE_HOST])) {
            return $_SERVER[REDIRECT_HTTP_MY_REMOTE_HOST];
    
        } else if (isset($_SERVER[HTTP_MY_REMOTE_ADDR])) {
            return $_SERVER[HTTP_MY_REMOTE_ADDR];
    
        } else if (isset($_SERVER[HTTP_MY_REMOTE_HOST])) {
            return $_SERVER[HTTP_MY_REMOTE_HOST];
    
        }
    }
    
    echo get_client_ip();
    

    .htaccess 中设置变量的灵感来自:http://www.askapache.com/htaccess/setenvif.html

    很高兴它终于奏效了!

  • 0

    您的.htaccess上的allow_url_include设置为1 . 这导致了问题,因为当你告诉PHP包含一个文件时,预处理器认为它是一个URL,其他人告诉你这个,所以让我们直截了当 .

    我的猜测是,只需禁用allow_url_include . 这是一个不好的做法,一个安全风险,例如,你无法用file_get_contents做任何事情 .

    如果您仍然确信包含远程文件是个好主意,那么您应该尝试使用完整路径,使用此包含Benjy1996给您的代码:

    include(getcwd().$tipo.".php");

    PD:你应该改变你的ip功能只能返回$ _SERVER ['REMOTE_ADDR'],因为它是你最信任的那个,因为它是最难伪造的 .

  • 1

    将它放在一个文件中,使用和不使用扩展名运行:

    <?php
    function get_ip_address() {
        return $_SERVER['REMOTE_ADDR'];
    }
        echo get_ip_address();
    
    ?>
    

    如果你运行上述内容,你会得到什么错误?

    编辑:你可以像这样编辑你的.htaccess,看看是否仍然出现错误:

    php_value allow_url_fopen on
    php_value allow_url_include 1
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !.(js|ico|txt|gif|bmp|jpeg|jpg|png|css|log|rss|zip|xml|sql|pdf|doc|docx|xls)$ $1.php [L]
    RewriteRule sitemap-categoria.xml$ sitemap.php?number=categoria
    RewriteRule sitemap-([0-9]+).xml$ sitemap.php?number=$1
    RewriteRule sitemap_index.xml$ sitemap_index.php
    

相关问题