Asked  2 Years ago    Answers:  5   Viewed   154 times

I'm using this PHP code to get a visitor's IP address:

<?php echo $_SERVER['REMOTE_ADDR']; ?>

But, I can't get the real IP address from visitors when they are using a proxy. Is there any way to get a visitor's IP address in this case?



Try this php code.


function getUserIP()
    // Get real visitor IP behind CloudFlare network
    if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) {
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];

    if(filter_var($client, FILTER_VALIDATE_IP))
        $ip = $client;
    elseif(filter_var($forward, FILTER_VALIDATE_IP))
        $ip = $forward;
        $ip = $remote;

    return $ip;

$user_ip = getUserIP();

echo $user_ip; // Output IP address [Ex:]

Wednesday, August 17, 2022

check out this site : Reading the battery level programmatically

but, carefully use. all of the APIs used here are undocumented on the iPhone, and will probably lead to a rejection if you submit this application to the App Store. Although battery charge status is not exactly, I'd recommend using the UIDevice battery monitoring methods.

Friday, October 21, 2022

The server is then accepting connections on an IPv6 socket. Some operating systems can do both IPv4 and IPv6 on an IPv6 socket. When that happens the IPv6 address will look like ::ffff: or ::ffff:c000:027b which is the same address but written in hexadecimal.

If you see IPv6 addresses like 2a00:8640:1::224:36ff:feef:1d89 then your webserver really is reachable over IPv6 :-)

Anyway, to convert everything back to a canonical form you can use something like:

// Known prefix
$v4mapped_prefix_hex = '00000000000000000000ffff';
$v4mapped_prefix_bin = pack("H*", $v4mapped_prefix_hex);

// Or more readable when using PHP >= 5.4
# $v4mapped_prefix_bin = hex2bin($v4mapped_prefix_hex); 

// Parse
$addr = $_SERVER['REMOTE_ADDR'];
$addr_bin = inet_pton($addr);
if( $addr_bin === FALSE ) {
  // Unparsable? How did they connect?!?
  die('Invalid IP address');

// Check prefix
if( substr($addr_bin, 0, strlen($v4mapped_prefix_bin)) == $v4mapped_prefix_bin) {
  // Strip prefix
  $addr_bin = substr($addr_bin, strlen($v4mapped_prefix_bin));

// Convert back to printable address in canonical form
$addr = inet_ntop($addr_bin);

Using this code, when you input one of the following:


you always get the canonical IPv4 address as output.

And of course IPv6 addresses get returned as canonical IPv6 addresses: 2a00:8640:0001:0000:0224:36ff:feef:1d89 becomes 2a00:8640:1::224:36ff:feef:1d89 etc.

Tuesday, September 6, 2022
[email protected]:~$ php -r "printf('%u', -931792879);"

There you go. My guess is that you are on a system with 32-bit ints and your ip_address_to_number is actually returning a float.

You see, with 32-bit ints, your maximum positive integer is (2^31) - 1 = 2 147 483 647, so the integer wraps around.

If you want to mimic the behaviour of the PHP function, do:

function ip_address_to_number($IPaddress) { 
 if(!$IPaddress) {
  return false;
 } else {
  $ips = split('.',$IPaddress);
  return($ips[3] | $ips[2] << 8 | $ips[1] << 16 | $ips[0] << 24);

(by the way, split has been deprecated)

Tuesday, September 20, 2022

Indeed, the Handler class object is unrelated to specific instances. Set up your own handler class, like this:

import SimpleHTTPServer
import SocketServer

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def handle_one_request(self):
        return SimpleHTTPServer.SimpleHTTPRequestHandler.handle_one_request(self)

print("Serving local directory")
httpd = SocketServer.TCPServer(("", 8080), MyHandler)

while True:
Sunday, October 23, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages