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?