0

I am trying to obtain client's time zone for one of my projects using PHP. Saw a post in stackoverflow with solutions for what I was looking for. Here's the link to that post.

get user timezone

I followed the most upvoted solution but it produced an error something like this.

enter image description here

Suppose $_SERVER['REMOTE_ADDR'] returns 212.88.207.202 to $ip, I hardcoded that value to $ip in my develpoment phase. It doesn't matter since hardcoding 212.88.207.202 or using $_SERVER['REMOTE_ADDR'] this returns the same value to $ip.

But when I run the page, I get this error.

Warning: file_get_contents(http://smart-ip.net/geoip-json/212.88.207.202): failed to open stream: No connection could be made because the target machine actively refused it.

Here's the code

<?php

$ip     = '212.88.207.202'; // means we got user's IP address
$json   = file_get_contents( 'http://smart-ip.net/geoip-json/' . $ip); // this one service we gonna use to obtain timezone by IP
// maybe it's good to add some checks (if/else you've got an answer and if json could be decoded, etc.)
$ipData = json_decode($json, true);
//var_dump($ipData);
if ($ipData['timezone']) {
    $tz = new DateTimeZone( $ipData['timezone']);
    $now = new DateTime( 'now', $tz); // DateTime object corellated to user's timezone
} else {
    // we can't determine a timezone - do something else...
}
?>

Where did I go wrong???

Community
  • 1
  • 1
Smokey
  • 1,857
  • 6
  • 33
  • 63
  • The target machine has dropped your connection due to some conditons. This can have many reasons depending on the service conditions. Your IP might be blacklisted for some reason, or the service is not available at the moment. It is also possible that you are passing the parameters incorrect. If you dont post some more code of yours we cannot really help you here. – Steini Jul 15 '14 at 09:56
  • 1
    Thanks, well it looks like the problem is already at smart-ip.net I cannot really open it in my browser. What is this? Is this an open service or your own domain? – Steini Jul 15 '14 at 10:00
  • 2
    **actively refused your connection** usually means there's no webserver running on the target machine. It looks like that service is no longer running. – Barmar Jul 15 '14 at 10:00
  • @Steini It's a service that was recommended in the question he linked to: http://stackoverflow.com/a/10103105/1491895 – Barmar Jul 15 '14 at 10:01
  • If so, is there an alternative for smart-ip.net ??? @Barmar – Smokey Jul 15 '14 at 10:02
  • @The Joker, you have two possibilities, use Google and try to find an alternative service or what would be even better is write your own service in form of a PHP script. There are some whois lookup scripts with that you can determine the country matching to the ip and when you got the country you can easily determine the timezone. – Steini Jul 15 '14 at 10:02
  • 1
    It's an open service. @Steini – Smokey Jul 15 '14 at 10:02
  • There are pay services like MaxMind. – Barmar Jul 15 '14 at 10:03
  • @TheJoker It **was** an open service. Maybe they got overloaded and shut down. Now I think you'll need to use pay services like MaxMind. – Barmar Jul 15 '14 at 10:03
  • http://stackoverflow.com/questions/743505/how-to-get-time-zone-through-ip-address-in-php – Barmar Jul 15 '14 at 10:04
  • @Barmar this is not even nesessary. You basically just need to identify the users country and this can be achived using the REMOTE IP (atleast if there is no proxy in use). The other method using jQuery can also fail when NoScript is used or JavaScript is disabled so both ways will never work for 100% but I think an IP should be reliable enough... – Steini Jul 15 '14 at 10:11
  • 1
    @Steini There are 6 time zones in US and Canada, so you need to do more than identify the country. You also need to know the rules for when each country changes to DST. – Barmar Jul 15 '14 at 10:12

1 Answers1

1

Well.....the problem is with the target machine. I tried opening smart-ip.net in my browser, but I wasn't able to do so. I think they are no longer running their service. That is the reason why target machine actively refused my connection. Viola!

Smokey
  • 1,857
  • 6
  • 33
  • 63