NYCPHP Meetup

NYPHP.org

[nycphp-talk] php script

Guilherme Blanco guilhermeblanco at gmail.com
Fri Feb 22 09:19:54 EST 2008


HI,


Th right way to store IPs is by converting them to long using ip2long.
By doing it, you can check if it is valid (returns false on failure)
and also enable you a by region search.

There are services like geocode, ip2country, geoip, etc that gives you
a database of countries with their ip2long ranges.
All you have to do then is to get the column and search in this
country db. You are able to do joins (and use a single query) instead
of retrieve, convert, another query.

Also, I use this code to retrieve the IP address of visitor (please
notice that this may not work 100% of times, but it's better than a
single REMOTE_ADDR):


function _getIp()
{
	// This function get the IP of an user, even when he's behind a simple proxy
	// List of possible ip sources, in order of priority
	$ip_sources = array(
		"HTTP_X_FORWARDED_FOR",
		"HTTP_X_FORWARDED",
		"HTTP_FORWARDED_FOR",
		"HTTP_FORWARDED",
		"HTTP_X_COMING_FROM",
		"HTTP_COMING_FROM",
		"REMOTE_ADDR",
	);

	foreach ( $ip_sources as $ip_source ) {
		if ( isset( $_SERVER[$ip_source] ) ) {
			$proxy_ip = $_SERVER[$ip_source];
			break;
		}
	}

	// If the ip is still not found, try the getenv() function without
	// error reporting. This will automatically provide a FALSE result
	// on failure
	$proxy_ip = ( isset( $proxy_ip ) ) ? $proxy_ip : @getenv("REMOTE_ADDR");

	// Return the IP found
	return $proxy_ip;
}



I hope my $0.02 helped you a bit.


Regards,

On Fri, Feb 22, 2008 at 11:06 AM, Allen Shaw <ashaw at polymerdb.org> wrote:
> Radu Cirimpei wrote:
>  > If I use 'getenv("REMOTE_ADDR") ' for return the IP
>  > address of the
>  > person visiting my site, what is the best to do?
>  Hi Radu,
>
>  You can give us a little more detail, right?  Tell us, what are you
>  trying to accomplish -- that is, /why/ do you need the remote address,
>  and what are you going to use it for?
>
>  - Allen
>
>  --
>  Allen Shaw
>  slidePresenter (http://slides.sourceforge.net)
>
>  _______________________________________________
>  New York PHP Community Talk Mailing List
>  http://lists.nyphp.org/mailman/listinfo/talk
>
>  NYPHPCon 2006 Presentations Online
>  http://www.nyphpcon.com
>
>  Show Your Participation in New York PHP
>  http://www.nyphp.org/show_participation.php
>



-- 
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9166-6902
MSN: guilhermeblanco at hotmail.com
URL: http://blog.bisna.com
São Carlos - SP/Brazil


More information about the talk mailing list