Getting ip address with PHP

Carlos

Member
Registered
Joined
Sep 30, 2017
Messages
38
Points
0
Hello,

I'm trying to get my ip with:
PHP:
$_SERVER['REMOTE_ADDR'];
The thing is that i'm getting a different address with php than from websites like myip.com
I've read that there is no 100% reliable way to get an ip address, but what would be my best option using only php?
Thanks. :)
 

cheapservers

New member
Registered
Joined
Dec 27, 2017
Messages
1
Points
3
Hello,
You can try this:

PHP:
	if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
		$ip = $_SERVER['HTTP_CLIENT_IP'];
	} 
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
		$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	} 
	else {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	echo 'ip is: '.$ip;
 
Latest Threads
Replies
2
Views
16
Replies
0
Views
11
Replies
0
Views
184
Recommended Threads
Replies
6
Views
4,118
Replies
6
Views
4,938
Replies
9
Views
6,088
Replies
3
Views
3,317

Latest postsNew threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top