ALERT!
Click here to register with a few steps and explore all our cool stuff we have to offer!

Jump to content



Photo

[PHP] IP address script - An easy way to get a visitor's IP


  • Please log in to reply
[PHP] IP address script - An easy way to get a visitor's IP

#1

JavaQuality
JavaQuality
    Offline
    36
    Rep
    292
    Likes

    Veteran

Posts: 843
Threads: 77
Joined: Nov 17, 2019
Credits: 0

Four years registered
#1

Yes, this is nothing too advanced, but I think it's still somewhat useful for websites or programs.

 

Hidden Content
You'll be able to see the hidden content once you reply to this topic or upgrade your account.

Additionally, you can add a function that writes logged IPs into a text file if you wanna have an IP logger or implement this into a view blocker (which I did for whatever reason).

Yeah so whatever, it's some basic stuff, have fun with this.


  • 1

?url=https%3A%2F%2Fmedia.giphy.com%2Fmed


#2

JavaQuality
JavaQuality
    Offline
    36
    Rep
    292
    Likes

    Veteran

Posts: 843
Threads: 77
Joined: Nov 17, 2019
Credits: 0

Four years registered
#2

Nice release. Thanks!

Nice to see some appreciation! :hype:

Thanks, Sir.


  • 1

?url=https%3A%2F%2Fmedia.giphy.com%2Fmed


#3

Omphalophobia
Omphalophobia
    Offline
    0
    Rep
    17
    Likes

    Member

Posts: 70
Threads: 4
Joined: Oct 21, 2015
Credits: 0

Eight years registered
#3

Just a small note, there are other things you can check for the IP in case of things like proxies. This is the function I have used for the things I made in PHP.

function getRealIpAddr(){
		if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
		{
			$ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
		{
			$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else
		{
			$ip=$_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}

  • 0

#4

JavaQuality
JavaQuality
    Offline
    36
    Rep
    292
    Likes

    Veteran

Posts: 843
Threads: 77
Joined: Nov 17, 2019
Credits: 0

Four years registered
#4

 

Just a small note, there are other things you can check for the IP in case of things like proxies. This is the function I have used for the things I made in PHP.

function getRealIpAddr(){
		if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
		{
			$ip=$_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
		{
			$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		else
		{
			$ip=$_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}

That's really interesting.

Yeah I was thinking about making an Anti-VPN/Proxy in PHP to release it here.


  • 0

?url=https%3A%2F%2Fmedia.giphy.com%2Fmed


#5

natuanhn
natuanhn
    Offline
    0
    Rep
    0
    Likes

    Lurker

Posts: 9
Threads: 0
Joined: Jan 02, 2020
Credits: 0
Four years registered
#5
Thanks JavaQualyti

  • 0

#6

JavaQuality
JavaQuality
    Offline
    36
    Rep
    292
    Likes

    Veteran

Posts: 843
Threads: 77
Joined: Nov 17, 2019
Credits: 0

Four years registered
#6

hey there bro! 
this is usefull in some cases yea, but this will never work without risk. Unfortunately IPs (or remote-adresses) are easily spoofed. So if you want to get a clients IP, the best to use is the following function:

 

// Function to get the client ip address
function get_client_ip_env() {
    $ipaddress = '';
    if (getenv('HTTP_CLIENT_IP'))
        $ipaddress = getenv('HTTP_CLIENT_IP');
    else if(getenv('HTTP_X_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
    else if(getenv('HTTP_X_FORWARDED'))
        $ipaddress = getenv('HTTP_X_FORWARDED');
    else if(getenv('HTTP_FORWARDED_FOR'))
        $ipaddress = getenv('HTTP_FORWARDED_FOR');
    else if(getenv('HTTP_FORWARDED'))
        $ipaddress = getenv('HTTP_FORWARDED');
    else if(getenv('REMOTE_ADDR'))
        $ipaddress = getenv('REMOTE_ADDR');
    else
        $ipaddress = 'UNKNOWN';
 
    return $ipaddress;
} 

Now this uses the PHP getenv() function, this is completely fine but not ideal in all use-cases since getenv takes the values from the PHP environment. This is the $_SERVER version, where it takes the values from the webserver:

// Function to get the client ip address
function get_client_ip_server() {
    $ipaddress = '';
    if ($_SERVER['HTTP_CLIENT_IP'])
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    else if($_SERVER['HTTP_X_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    else if($_SERVER['HTTP_FORWARDED_FOR'])
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    else if($_SERVER['HTTP_FORWARDED'])
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    else if($_SERVER['REMOTE_ADDR'])
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    else
        $ipaddress = 'UNKNOWN';
 
    return $ipaddress;
}  

u can just paste that code before u actually want to use the variable, then just define the $ip variable to this:

 

$ip = get_client_ip_env();

// or 

$ip = get_client_ip_server(); 

hope u enjoy ;)

-Proof

Oh wow, thanks dude! :hype:


  • 0

?url=https%3A%2F%2Fmedia.giphy.com%2Fmed


#7

JavaQuality
JavaQuality
    Offline
    36
    Rep
    292
    Likes

    Veteran

Posts: 843
Threads: 77
Joined: Nov 17, 2019
Credits: 0

Four years registered
#7

No problem :)
Drop me a friend-request on Discord if u need any further help!

Alrighty.


  • 0

?url=https%3A%2F%2Fmedia.giphy.com%2Fmed


#8

jediburaniju
jediburaniju
    Offline
    0
    Rep
    0
    Likes

    Lurker

Posts: 6
Threads: 0
Joined: Jan 03, 2020
Credits: 0
Four years registered
#8

hmmmmm interesting


  • 0

#9

JavaQuality
JavaQuality
    Offline
    36
    Rep
    292
    Likes

    Veteran

Posts: 843
Threads: 77
Joined: Nov 17, 2019
Credits: 0

Four years registered
#9

Thank you bro :D

Np.


  • 0

?url=https%3A%2F%2Fmedia.giphy.com%2Fmed


#10

femiy73190
femiy73190
    Offline
    0
    Rep
    1
    Likes

    New Member

Posts: 17
Threads: 0
Joined: Jan 24, 2020
Credits: 0

Four years registered
#10

thanks for script dude nice job


  • 0


 Users browsing this thread: