Click here to get back home

using string functions

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
using string functions Sudhakar 07-14-2008
Get Chitika Premium
Posted by Sudhakar on July 14, 2008, 4:33 am
Please log in for more thread options
i am using $ip= $_SERVER['REMOTE_ADDR'] to retrieve the ip address of
the client for example if the value returned from $ip is
50.160.190.150

i would like to find out which country the request has come from. i
believe by using the third set of numbers (in this case=190) from an
ip address we can find out the country name. i can declare a variable
with a list of country names and the range of values, what i need help
is to extract the 3rd set of numbers from $ip.

as ip address keep changing the 3rd set of numbers can be single,
double or 3 digit number how can we extract the number that is stored
in $ip after the second dot and before the third dot

please advice.

thanks.

Posted by sathyashrayan on July 14, 2008, 5:08 am
Please log in for more thread options

>i am using $ip= $_SERVER['REMOTE_ADDR'] to retrieve the ip address of
> the client for example if the value returned from $ip is
> 50.160.190.150
>
> i would like to find out which country the request has come from. i
> believe by using the third set of numbers (in this case=190) from an
> ip address we can find out the country name. i can declare a variable
> with a list of country names and the range of values, what i need help
> is to extract the 3rd set of numbers from $ip.
>
> as ip address keep changing the 3rd set of numbers can be single,
> double or 3 digit number how can we extract the number that is stored
> in $ip after the second dot and before the third dot
>
> please advice.
>
> thanks.

have a look at this..

http://www.phpclasses.org/browse/package/1477.html

I see explode used to extract the "."

Hope this helps..



Posted by Claudio Corlatti on July 14, 2008, 7:47 am
Please log in for more thread options
> have a look at this..
>
> http://www.phpclasses.org/browse/package/1477.html
>
> I see explode used to extract the "."
>
> Hope this helps..

yes, i agree with sathyashrayan, explode is what you need

$ip = $_SERVER['REMOTE_ADDR'];
$array_ip = explode('.',$ip);
$third_octet = $ip[2]; //remember, array = zero based


Claudio

Posted by thinkdj on July 14, 2008, 10:50 am
Please log in for more thread options
> i am using $ip=3D $_SERVER['REMOTE_ADDR'] to retrieve the ip address of
> the client for example if the value returned from $ip is
> 50.160.190.150

To get the 3rd Octet, you may the string as the previous
posters have said...

But if you have to get the Country Information, you'll have to get if
from a database.. Entire DBs like IP2Location etc. can be purchased..
(commecial, very accurate results)

Also, Try HostIP to get country informtion. They have a good API..
[FREE]
First, get the IP using $ip=3D $_SERVER['REMOTE_ADDR'];
Get the country and city name with
http://api.hostip.info/get_html.php?ip=3D$_SERVER['REMOTE_ADDR']
Try an example http://api.hostip.info/get_html.php?ip=3D222.88.238.16 -
This will show and example of the output format. Get the text contents
using $data =3D @file_get_contents($_GET['url']); or something and
extract the info from there//

Try that .. Hope it helps...

Cheers !
Dj






>
> i would like to find out which country the request has come from. i
> believe by using the third set of numbers (in this case=3D190) from an
> ip address we can find out the country name. i can declare a variable
> with a list of country names and the range of values, what i need help
> is to extract the 3rd set of numbers from $ip.
>
> as ip address keep changing the 3rd set of numbers can be single,
> double or 3 digit number how can we extract the number that is stored
> in $ip after the second dot and before the third dot
>
> please advice.
>
> thanks.



> i am using $ip=3D $_SERVER['REMOTE_ADDR'] to retrieve the ip address of
> the client for example if the value returned from $ip is
> 50.160.190.150
>
> i would like to find out which country the request has come from. i
> believe by using the third set of numbers (in this case=3D190) from an
> ip address we can find out the country name. i can declare a variable
> with a list of country names and the range of values, what i need help
> is to extract the 3rd set of numbers from $ip.
>
> as ip address keep changing the 3rd set of numbers can be single,
> double or 3 digit number how can we extract the number that is stored
> in $ip after the second dot and before the third dot
>
> please advice.
>
> thanks.


Posted by Claudio Corlatti on July 14, 2008, 3:28 pm
Please log in for more thread options
> First, get the IP using $ip= $_SERVER['REMOTE_ADDR'];
> Get the country and city name
withhttp://api.hostip.info/get_html.php?ip=$_SERVER['REMOTE_ADDR']
> Try an examplehttp://api.hostip.info/get_html.php?ip=222.88.238.16-
> This will show and example of the output format. Get the text contents
> using $data = @file_get_contents($_GET['url']); or something and
> extract the info from there//

what a nice tip! thanks for that!



Similar ThreadsPosted
$string .= $new vs $string = $string . $new performance July 3, 2008, 6:41 pm
XML DOM Functions in PHP January 17, 2006, 5:06 am
&$ in functions November 30, 2007, 4:13 am
using header functions in php August 12, 2004, 1:29 pm
Variables Outside Functions June 2, 2005, 11:23 am
The use of fn: functions in XSL - XPath June 23, 2005, 9:14 pm
some useful custom functions! June 28, 2005, 1:47 pm
Are there static functions in PHP? September 10, 2005, 4:35 pm
win32service Functions October 28, 2005, 12:48 pm
Calling C functions in PHP November 27, 2005, 11:41 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap