简体   繁体   中英

PHP - Get IP when page calling by htaccess

I have a website in PHP and need to know what the IP of the accessing client is. For this I am using the function below:

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;
}

But when accessing the site by url omenorpreco.com/teste the page returns the server IP.

When access to page the url omenorpreco.com/teste.php, the page returns the client IP.

Possibly this error occurs because when you access the page without the extension ".php", the server interprets the page by .htaccess?

How can I adjust my application for both accesses, return the client's IP, and not the server IP?

above my htaccess code


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

and url_amigavel.php code

<?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";

}

?>

EDIT: CAN I SET GLOBAL_VAR IN HTACCESS WITH THE CLIENT IP?

Put this in a file and run with and without the extension:

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

?>

What error do you get if you run the above?

EDIT: Can you edit your .htaccess like this and see if the error still occurs:

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

I think I just saw it work on your server! Change .htaccess to;

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

An change test.php to;

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();

Inspiration for setting variables in .htaccess from: http://www.askapache.com/htaccess/setenvif.html

Glad it finally worked!

You have allow_url_include set to 1 on your .htaccess. This is causing problems because as you are telling PHP to include a file, the preprocessor thinks it's a URL, other people have told you this so let's go straight.

My guess is, just disable allow_url_include. It's a bad practice, a security risk and there's nothing you can't do just with file_get_contents for example.

If you are still convinced that including remote files is a good idea, you should try using a full path, using this include with the code Benjy1996 gave you:

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

PD: you should probably change your ip function to return $_SERVER['REMOTE_ADDR'] only as well, as it's the one you can trust the most, because it's the most difficult to fake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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