Honesty Rocks! truth rules.

Getting Ip

HOME      >>       Programming

khalilov

Whats the php code to get the users IP, i want to start a referal program and i don't want to people to refer themselves:P


FirefoxRocks

The IP address is stored in a $_SERVER[] variable, in this case it is the REMOTE_ADDR variable. So it would be:

$_SERVER['remote_addr'];

If you want to store that in a variable (which you probably can figure out) and display it, you do this:
$ip = $_SERVER['remote_addr'];echo($ip);


Jeigh1405241495

You've probably already thought of this, but you might want to store a long term cookie as a secondary check. IP's don't change a lot anymore but they can still depending on the connection type or the users dedication lol, so adding a little cookie when they register just sort of adds a second layer that they would need to think to clear in order to get around your checks. Then again if it's not that important and you just want to limit it from happening frequently then the IP thing would be more then enough obviously.


sparkx

Just a side message there is NO way to track internet users other then real-time IP lookup or backtracking (which is very hard to do and is usually only used in movies). I would say probably not to even bother making a script like this but rather only accept the referral if the new user is active (for example if it is a forum then make a minimum of 10 posts and remove the referral if the new user gets banned for spamming est.). If one person wants to do that much work just to get a referral then let them have it... Remember Cookies can be edited or deleted and IPs can be changed in less then 30 seconds.Hope this helps,Sparkx


levimage

Do you mean like how some online messenger/chat(s) detect if more than one user logs in or is logged in from an IP address or if someone from a specific ip is creating multiple user accounts within a certain time period. Maybe you should just have like a cool off period verses denying or banning someone. Cause their may be multiple people using your services from the same place like a pubic wifi, school, or building.Just something to think about.


Quatrux

Here is also a way to get the ip address from a user using php language, even though this is not the best method, but I am using it and don't really have problems, I will show this example in a class, those who know php could easily just change it or something like that :mellow:

class MyClass { /** * @access public * @variables */ var $ipAddress, $proxyAddress = ''; function setIPAddress() { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { if (isset($_SERVER['HTTP_CLIENT_IP'])) { $this->proxyAddress = $_SERVER['HTTP_CLIENT_IP']; } else { $this->proxyAddress = $_SERVER['REMOTE_ADDR']; } $this->ipAddress = $_SERVER['HTTP_X_FORWARDED_FOR']; } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $this->ipAddress = $_SERVER['HTTP_CLIENT_IP']; } else { $this->ipAddress = $_SERVER['REMOTE_ADDR']; } }}

and whenever you want to show the ip address you just do:

$MyClass = new MyClass();

and you can call the function once like $Myclass->setIPAddress(); and you'll get the value and you could use it like this:

echo $MyClass->ipAddress or echo $MyClass->proxyAddress but I would do it in a constructor of that class, the class constructor is a function/method of that class and needs to have the same name as the class in other languages it also can be init() like on Zend framework on PHP.. A constructor here is good because this function needs to be done/executed/called only once, also it could be done differently as I said, but this is just one of a lot of methods which is possible.