Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Detect user location with php


    Hi;

    What is the "proper" way of detecting user's country and city?

    There are some examples but they are not exact. For example I am on the Gold Coast but this says I am from Sydeny!



    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    if you expect to get perfectly accurate results: Forget about it. Using only the IP (like you do) is particularly inaccurate. The HTML5 geolocation API may give better results, because it works client-side and can use several factors. However, it will not always be available, so you'll need server-side location as a fallback.

    Either way, don't expect to pinpoint the exact location of the PC. And of course geolocation only works if the users want to be located. As soon as people turn off client-side geolocation and use proxies or VPNs, the only location you'll get is somewhere in China.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Location
    22 rue Foch,5070 Akwa Douala Cameroon
    Posts
    9
    Rep Power
    0

    Detect user location with php


    You can detect the user location for using IP tracing. And detect the help of HTML5 Geolocation API .
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    Cant you look at the IP addresses and determine what country you are from? sites such as cbs.com abc.com wont allow you to watch their shows online unless you live in US.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by paulh1983
    Cant you look at the IP addresses and determine what country you are from?
    Um, that's exactly what he did, and he already got the correct country. His issue is getting the right city. And that's when you need to factor in more data (as explained above).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  10. #6
  11. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    OK this is not how I thought it is. So I say what exatlly want to do:

    I want to make a simple reminder site. Anyone can go to that page, add their email, phone number, date and a few words about something they want to be reminded about and the system starts contatacting them on specific times.

    I want them to have the option to choose when they be reminded. So that the system sends them reminder in specific times. For example: when it is 5 hours to the time, remind me every hour.

    For example. My server is in Sydney and a visitor is in Ny. If they set a reminder for 5 pm 20th of Dec, that is euqual to 9 am 21st of Dec in Sydney.

    I set a reminder and I want the system to start contacting me every hour starting 2 days to the date.

    Makes sense? Let's talk about I might be over complicating it.
  12. #7
  13. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    get them to choose their timezone on signup, and then convert that time to a uniform time in the DB (say GMT+10 for syd or even better plain old UTC).

    Run with a uniform timezone in your DB, and then convert all input / display back to the user's timezone when the enter anything.

    User says "I am in europe (GMT+2 and want a reminder at 4pm", you just store that in the DB as 1400 UTC or whatever standard time you are using. Don't forget that daylight savings will impact on this conversion too. Then when your script runs to send reminders, just make sure date_default_timezone_set() is used to make sure your script and your DB share the same view of the current time and pick out which ones need reminders sent to them etc.

    tldr; get user to tell you their timezone, then convert all times to one standard timezone and store in the DB (don't forget to account for daylight savings if applicable).
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    @ English Breakfast Tea:

    And why don't you simply ask your users about their timezone?

    Of course you might try to make an educated guess so that you can set a default value. But do not rely on it. As I already tried to explain, people today do not always access the Internet directly from their home PC. Never heard of proxies, VPNs or Tor? Those are specifically made to obfuscate the original IP address. If you remind your users based on the timezone of the IP address, they'll be reminded at 5 pm in Sweden or something.

    To set a default value, first try to call Date.getTimezoneOffset() in JavaScript. If JavaScript is turned off, use IP-based geolocation as a fallback. And then ask the user to correct your guess.

    But I repeat: Do not rely on IP addresses or any other data you've gained. That's just naive. The only reliable source is the user. Only they can tell you their actual data -- if they want to.



    @ sir_drinxalot:

    It's not enough to simply store the target time. You must also save the selected timezone, because the timezone can change due to daylight saving time.

    Comments on this post

    • sir_drinxalot agrees : i agree, my post was more to say just ask them and then standardize that data. I briefly touched on daylight savings.
    Last edited by Jacques1; December 2nd, 2013 at 11:01 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Same issue


    Where you able to figure it out? I am in need of the same thing.
  18. #10
  19. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    if you expect to get perfectly accurate results: Forget about it.
    Hi;

    I am looking at Facebook and it shows that you're wrong.

    Facebook does show "EXACT" location of the place a member had sent a message. It sows it on map.

    Thanks
  20. #11
  21. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,102
    Rep Power
    1990
    Originally Posted by English Breakfast Tea
    Facebook does show "EXACT" location of the place a member had sent a message. It sows it on map.
    You know that 99.999% of the posts there that show location like that are done using mobile devices (and using FaceBooks app) and that's where the GPS data comes from? Not from a desktop/laptop browser.
  22. #12
  23. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Originally Posted by English Breakfast Tea
    Hi;

    I am looking at Facebook and it shows that you're wrong.

    Facebook does show "EXACT" location of the place a member had sent a message. It sows it on map.

    Thanks
    The real world called and infact showed that you are indeed wrong about him being wrong. geo ip lookups are no where near 100% accurate. I use mobile broadband a lot, and my location shows up as up to 300Km away from where I actually am.

    There is no guarantee that because a device in sydney has an IP, that the IP will still be in use by that device, or in that location for any period of time.
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  24. #13
  25. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by sir_drinxalot
    The real world called and infact showed that you are indeed wrong about him being wrong. geo ip lookups are no where near 100% accurate. I use mobile broadband a lot, and my location shows up as up to 300Km away from where I actually am.

    There is no guarantee that because a device in sydney has an IP, that the IP will still be in use by that device, or in that location for any period of time.
    It shows EXACT location on map. EXACT location. Try Facebook's message and you see it.
  26. #14
  27. Known to taste like chicken
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Location
    In front of my computer
    Posts
    399
    Rep Power
    311
    Originally Posted by English Breakfast Tea
    It shows EXACT location on map. EXACT location. Try Facebook's message and you see it.
    Yes, as catacaustic said, that will be via GPS, not via the user's IP. Facebook has me being about 50KM away from my house when I use 4G. It is accurate with GPS, but all I have to do is not allow the facebook app to use GPS and then the results are not correct. To really mess with the results, I just need to connect to a VPN or use a proxy and suddenly I am on a whole different continent.

    Moral of the story, you will never know for certain where your user is without asking them (and even then they can still lie).
    "Take thy beak from out my heart, and take thy form from off my door" - Homer J Simpson / Edgar Allan Poe

    Looking for a project Idea?
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0
    You can detect the user location for using IP address.
    Use geoip.php - GeoIP Country Whois Locator - PHP Classes.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo