GEO IP Code Expert Needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RummyBoy
    Confirmed User
    • Dec 2009
    • 2157

    #1

    Tech 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
    Raise Your Weapon
    • Jun 2003
    • 15601

    #2
    Originally posted by RummyBoy
    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;
    ?>

    Comment

    • k0nr4d
      Confirmed User
      • Aug 2006
      • 9231

      #3
      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.
      Mechanical Bunny Media
      Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

      Comment

      • AdultKing
        Raise Your Weapon
        • Jun 2003
        • 15601

        #4
        Originally posted by k0nr4d
        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.

        Comment

        • k0nr4d
          Confirmed User
          • Aug 2006
          • 9231

          #5
          Originally posted by AdultKing
          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.
          Mechanical Bunny Media
          Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

          Comment

          • AdultKing
            Raise Your Weapon
            • Jun 2003
            • 15601

            #6
            Originally posted by k0nr4d
            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

            Comment

            • RummyBoy
              Confirmed User
              • Dec 2009
              • 2157

              #7
              any clues people?

              Comment

              • k0nr4d
                Confirmed User
                • Aug 2006
                • 9231

                #8
                Originally posted by RummyBoy
                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";
                }
                ?>
                Mechanical Bunny Media
                Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                Comment

                • RummyBoy
                  Confirmed User
                  • Dec 2009
                  • 2157

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

                  Originally posted by k0nr4d
                  IF your geoip module is properly configured...

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

                  Comment

                  • AdultKing
                    Raise Your Weapon
                    • Jun 2003
                    • 15601

                    #10
                    Originally posted by RummyBoy
                    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);

                    Comment

                    • sarettah
                      see you later, I'm gone
                      • Oct 2002
                      • 14297

                      #11
                      Originally posted by RummyBoy
                      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.

                      .
                      All cookies cleared!

                      Comment

                      • daddy_fu
                        Registered User
                        • Aug 2015
                        • 15

                        #12
                        Originally posted by sarettah
                        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.
                        Lead Developer - Nudeflix

                        Comment

                        • AdultKing
                          Raise Your Weapon
                          • Jun 2003
                          • 15601

                          #13
                          Originally posted by sarettah
                          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.

                          Comment

                          • RummyBoy
                            Confirmed User
                            • Dec 2009
                            • 2157

                            #14
                            Originally posted by sarettah
                            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.

                            Comment

                            • AdultKing
                              Raise Your Weapon
                              • Jun 2003
                              • 15601

                              #15
                              Originally posted by RummyBoy
                              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.

                              Comment

                              • sarettah
                                see you later, I'm gone
                                • Oct 2002
                                • 14297

                                #16
                                Originally posted by AdultKing
                                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

                                Originally posted by RummyBoy
                                Yep, kindergarten level was needed in fact. I should have told you guys im clueless.
                                Nothing wrong with Kindergarten code

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

                                +1

                                .
                                All cookies cleared!

                                Comment

                                Working...