GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech GEO IP Code Expert Needed (https://gfy.com/showthread.php?t=1172086)

RummyBoy 08-13-2015 05:35 AM

GEO IP Code Expert Needed
 
Its a simple GEO IP which is intended to pull the City name from the database according to IP but if it cannot determine the City name, it should show just "Your City". GEO IP module is enabled but this code isn't working.

Can someone please show me what I'm doing wrong here?


PHP Code:

<?php
$geo
=geoip_record_by_name ($_SERVER['REMOTE_ADDR']);
echo 
$geo['city'];
if (
$geo == "") {
   
$geo['city'] = "Your City";
?>


AdultKing 08-13-2015 05:40 AM

Quote:

Originally Posted by RummyBoy (Post 20549714)
Its a simple GEO IP which is intended to pull the City name from the database according to IP but if it cannot determine the City name, it should show just "Your City". GEO IP module is enabled but this code isn't working.

Can someone please show me what I'm doing wrong here?


PHP Code:

<?php
$geo
=geoip_record_by_name ($_SERVER['REMOTE_ADDR']);
echo 
$geo['city'];
if (
$geo == "") {
   
$geo['city'] = "Your City";
?>



You are trying to compare an array as a string on line 4.

You need to address the key in the array to get the string.

Here's a sample from PHP.NET

Code:

<?php
# Collect a specific users GEOIP info
$info = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
print_r ($info);

# To get the info from one specific field
$country = $info['country_name'];
echo $country;

# To combine information from the array into a string
$info = implode("/", $info);
echo $info;
?>


k0nr4d 08-13-2015 06:06 AM

Do
Code:

<? print_r($_SERVER); ?>
If you don't see your country listed, it means the module or database for it is not properly configured.

I don't remember the exact variable, but you should be able to access the country with $_SERVER['GEOIP_COUNTRY_NAME'] or something like that - it will be listed in that $_SERVER dump.

AdultKing 08-13-2015 06:16 AM

Quote:

Originally Posted by k0nr4d (Post 20549727)
Do
Code:

<? print_r($_SERVER); ?>
If you don't see your country listed, it means the module or database for it is not properly configured.

I don't remember the exact variable, but you should be able to access the country with $_SERVER['GEOIP_COUNTRY_NAME'] or something like that - it will be listed in that $_SERVER dump.

But even then

Code:

if ($geo == "") {
  $geo['city'] = "Your City";
?>

won't work will it ?

I'm not on my dev machine so I can't try it.

k0nr4d 08-13-2015 06:48 AM

Quote:

Originally Posted by AdultKing (Post 20549736)
But even then

Code:

if ($geo == "") {
  $geo['city'] = "Your City";
?>

won't work will it ?

I'm not on my dev machine so I can't try it.

It's not "correct" but php is very forgiving, it won't error out and just in that isolated piece of code will work. He should be comparing to $geo['city'] though.

AdultKing 08-13-2015 07:01 AM

Quote:

Originally Posted by k0nr4d (Post 20549756)
It's not "correct" but php is very forgiving, it won't error out and just in that isolated piece of code will work. He should be comparing to $geo['city'] though.

Im pedantic :)

RummyBoy 08-13-2015 08:06 PM

any clues people?

k0nr4d 08-14-2015 01:07 AM

Quote:

Originally Posted by RummyBoy (Post 20550390)
any clues people?

IF your geoip module is properly configured...

Code:

<?php
$geo = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
if(!$geo['city']) {
  $geo['city'] = "Your City";
}
?>


RummyBoy 08-14-2015 04:25 AM

Well my code shows me a city.... your code doesnt work for me at all.
Doesn't show any city.

Quote:

Originally Posted by k0nr4d (Post 20550490)
IF your geoip module is properly configured...

Code:

<?php
$geo = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
if(!$geo['city']) {
  $geo['city'] = "Your City";
}
?>



AdultKing 08-14-2015 05:24 AM

Quote:

Originally Posted by RummyBoy (Post 20550571)
Well my code shows me a city.... your code doesnt work for me at all.
Doesn't show any city.

Can you please post the output from the following code.

Code:

$geo = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
echo  $geo['city'];

and

Code:

$geo = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
print_r($geo);


sarettah 08-14-2015 07:25 AM

Quote:

Originally Posted by RummyBoy (Post 20550571)
Well my code shows me a city.... your code doesnt work for me at all.
Doesn't show any city.

That is because K0nrad did NOT include an echo in there. I think he assumed you knew to echo the output but after reading the thread I don't know that you do. So, without meaning anything derogatory here, let's take this to kindergarten level.

Your code:

$geo=geoip_record_by_name ($_SERVER['REMOTE_ADDR']);
echo $geo['city'];
if ($geo == "") {
$geo['city'] = "Your City";
}

You are echoing the city before you test to see if city is filled. So you see whatever city the geo returned. You might also see nothing come back if the city is blank. You will NEVER see "Your City" come back because you are pushing the output before the if statement. Does that make sense to you?

Also K0nrad, AK and I think you have a mistake in the if statement. You are checking against geo (if geo="") not against geo['city']. You should be comparing against city.

K0nrad fixed the if statement for you but he removed the echo (which was in the wrong place). So let's try K0nrads code with an echo added:

$geo = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
if(!$geo['city']) {
$geo['city'] = "Your City";
}
echo $geo['city'];

That should show the city if geo_ip returned a city and should show "Your City" if geo_ip['city'] comes back null or empty.

See if that works for you or not.

.

daddy_fu 08-14-2015 07:51 AM

Quote:

Originally Posted by sarettah (Post 20550658)
That is because K0nrad did NOT include an echo in there. I think he assumed you knew to echo the output but after reading the thread I don't know that you do.

For a second there I was ready to jump in and see if I could help, but after RummyBoy's last reply I can see it's a lost cause.

AdultKing 08-14-2015 08:00 AM

Quote:

Originally Posted by sarettah (Post 20550658)
Also K0nrad, AK and I think you have a mistake in the if statement. You are checking against geo (if geo="") not against geo['city']. You should be comparing against city.

Yeah I said that, read up where I said I was pedantic. :)

I think it was lost on him.

RummyBoy 08-14-2015 08:02 AM

Quote:

Originally Posted by sarettah (Post 20550658)
So, without meaning anything derogatory here, let's take this to kindergarten level.

Yep, kindergarten level was needed in fact. I should have told you guys im clueless.

I haven't been able to test the default "Your City" but this version does actually seem to work.

Thanks for all your help people.

AdultKing 08-14-2015 08:04 AM

Quote:

Originally Posted by RummyBoy (Post 20550683)
Yep, kindergarten level was needed in fact. I should have told you guys im clueless.

Nah, you're not. You were smart enough to ask the question and the only dumb question is one you don't ask.

:thumbsup

sarettah 08-14-2015 09:30 AM

Quote:

Originally Posted by AdultKing (Post 20550682)
Yeah I said that, read up where I said I was pedantic. :)

Ooops, I read that as "Im pathetic". Sorry about that. My bad ;p

Quote:

Originally Posted by RummyBoy (Post 20550683)
Yep, kindergarten level was needed in fact. I should have told you guys im clueless.

Nothing wrong with Kindergarten code :thumbsup

Quote:

Originally Posted by AdultKing (Post 20550685)
Nah, you're not. You were smart enough to ask the question and the only dumb question is one you don't ask.

:thumbsup

+1 :thumbsup

.


All times are GMT -7. The time now is 09:21 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123