Simple PHP function. First one to give the correct advice will get the $5 via paypal

Discussion in 'Programming' started by sowers, May 27, 2012.

  1. #1
    Hello,

    First one to give the correct advice will get the $5 via paypal

    my website allows you to signup some countries

    I use the api: http://www.hostip.info/

    but this service was discontinued,

    I need to modify this code below to work with this site:

    http://freegeoip.net

    https://github.com/fiorix/freegeoip/blob/master/README.rst



    	
    	
    	if ($PREFS->conf['allow_registration'] != 1)
    	{
    		$TEMPLATE->set_message("info", ($LANG['register']['no_registration']), 0, 1);
    		return 0;
    	}
    
    
    	$ipaddress = $SESSION->get_ip();
    	if ( !isset($_SESSION['ipcountry']) ) {
    		if ( $ipaddress != '127.0.0.1' ) {
    			$_SESSION['ipcountry'] = send_remote_request('http://api.hostip.info', '/get_html.php?ip='.$ipaddress);
    		}
    		else {
    			$_SESSION['ipcountry'] = '--';
    		}
    	}
    	if ( strpos($_SESSION['ipcountry'], '(CA)') === false &&
    		strpos($_SESSION['ipcountry'], '(GB)') === false &&
    		strpos($_SESSION['ipcountry'], '(AU)') === false &&
    		strpos($_SESSION['ipcountry'], '(NZ)') === false &&
    		strpos($_SESSION['ipcountry'], '(US)') === false )
    	{
    		$TEMPLATE->set_message("error", "We can accept registrations only from these countries: United States, UK, Canada, Australia, New Zealand", 0, 1);
    		return 0;
    	}
    
    	
    PHP:
     
    sowers, May 27, 2012 IP
  2. Doxeay

    Doxeay Greenhorn

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #2
    I think this should work

    EDIT

    Change json to csv or xml to change the result format.
     
    Last edited: May 27, 2012
    Doxeay, May 27, 2012 IP
  3. sowers

    sowers Well-Known Member

    Messages:
    257
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #3
    sowers, May 27, 2012 IP
  4. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #4
    
    <?php
    if ($PREFS->conf['allow_registration'] != 1) {
        $TEMPLATE->set_message("info", ($LANG['register']['no_registration']), 0, 1);
        return 0;
    }
    
    $ipaddress = $SESSION->get_ip();
    if (!isset($_SESSION['ipcountry'])) {
        if ($ipaddress != '127.0.0.1') { 
            // $_SESSION['ipcountry'] = send_remote_request('http://api.hostip.info', '/get_html.php?ip=' . $ipaddress);
            $temp = json_decode(file_get_contents('http://freegeoip.net/json' . $ipaddress));
            $_SESSION['ipcountry'] = $temp->country_code;
        } else {
            $_SESSION['ipcountry'] = '--';
        }
    }
    if (strpos($_SESSION['ipcountry'], '(CA)') === false &&
            strpos($_SESSION['ipcountry'], '(GB)') === false &&
            strpos($_SESSION['ipcountry'], '(AU)') === false &&
            strpos($_SESSION['ipcountry'], '(NZ)') === false &&
            strpos($_SESSION['ipcountry'], '(US)') === false) {
        $TEMPLATE->set_message("error", "We can accept registrations only from these countries: United States, UK, Canada, Australia, New Zealand", 0, 1);
        return 0;
    }
    
    PHP:
     
    Einheijar, May 27, 2012 IP
  5. sowers

    sowers Well-Known Member

    Messages:
    257
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #5
    I tested, It does not work.
     
    sowers, May 27, 2012 IP
  6. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #6
    
    <?php
    if ($PREFS->conf['allow_registration'] != 1) {
        $TEMPLATE->set_message("info", ($LANG['register']['no_registration']), 0, 1);
        return 0;
    }
    
    $ipaddress = $SESSION->get_ip();
    if (!isset($_SESSION['ipcountry'])) {
        if ($ipaddress != '127.0.0.1') { 
            // $_SESSION['ipcountry'] = send_remote_request('http://api.hostip.info', '/get_html.php?ip=' . $ipaddress);
            $temp = json_decode(file_get_contents('http://freegeoip.net/json/' . $ipaddress));
            $_SESSION['ipcountry'] = $temp->country_code;
        } else {
            $_SESSION['ipcountry'] = '--';
        }
    }
    if (strpos($_SESSION['ipcountry'], '(CA)') === false &&
            strpos($_SESSION['ipcountry'], '(GB)') === false &&
            strpos($_SESSION['ipcountry'], '(AU)') === false &&
            strpos($_SESSION['ipcountry'], '(NZ)') === false &&
            strpos($_SESSION['ipcountry'], '(US)') === false) {
        $TEMPLATE->set_message("error", "We can accept registrations only from these countries: United States, UK, Canada, Australia, New Zealand", 0, 1);
        return 0;
    }
    
    PHP:
     
    Einheijar, May 27, 2012 IP
  7. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #7
    Missed out a backslash after the json.
     
    Einheijar, May 27, 2012 IP
  8. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #8
    Try this instead
    
    if ($PREFS->conf['allow_registration'] != 1) {
        $TEMPLATE->set_message("info", ($LANG['register']['no_registration']), 0, 1);
        return 0;
    }
    
    $ipaddress = $SESSION->get_ip();
    if (!isset($_SESSION['ipcountry'])) {
        if ($ipaddress != '127.0.0.1') { 
            // $_SESSION['ipcountry'] = send_remote_request('http://api.hostip.info', '/get_html.php?ip=' . $ipaddress);
            $temp = json_decode(file_get_contents('http://freegeoip.net/json/' . $ipaddress));
            $_SESSION['ipcountry'] = $temp->country_code;
        } else {
            $_SESSION['ipcountry'] = '--';
        }
    }
    if (strpos($_SESSION['ipcountry'], 'CA') === false &&
            strpos($_SESSION['ipcountry'], 'GB') === false &&
            strpos($_SESSION['ipcountry'], 'AU') === false &&
            strpos($_SESSION['ipcountry'], 'NZ') === false &&
            strpos($_SESSION['ipcountry'], 'US') === false) {
        $TEMPLATE->set_message("error", "We can accept registrations only from these countries: United States, UK, Canada, Australia, New Zealand", 0, 1);
        return 0;
    }
    
    PHP:
     
    Einheijar, May 27, 2012 IP
  9. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #9
    Please try this:

    
    if ($PREFS->conf['allow_registration'] != 1)
        {
            $TEMPLATE->set_message("info", ($LANG['register']['no_registration']), 0, 1);
            return 0;
        }
    
    
        $ipaddress = $SESSION->get_ip();
        if ( !isset($_SESSION['ipcountry']) ) {
            if ( $ipaddress != '127.0.0.1' ) {
            	$json = json_decode(str_replace('\"','"',@file_get_contents('http://freegeoip.net/json/'.$ipaddress)),true);
                $_SESSION['ipcountry'] = $json['country_code'];
            }
            else {
                $_SESSION['ipcountry'] = '--';
            }
        }
        if (!in_array($SESSION['ipcountry'],array('CA','GB','AU','NZ','US')))
        {
            $TEMPLATE->set_message("error", "We can accept registrations only from these countries: United States, UK, Canada, Australia, New Zealand", 0, 1);
            return 0;
        }
    
    PHP:
     
    koko5, May 28, 2012 IP
  10. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    As Seller:
    100% - 0
    As Buyer:
    100% - 0
    #10
    Here is variant 2 using GeoIP XML output and simple xml:

    
    if ($PREFS->conf['allow_registration'] != 1)
        {
            $TEMPLATE->set_message("info", ($LANG['register']['no_registration']), 0, 1);
            return 0;
        }
    
    
        $ipaddress = $SESSION->get_ip();
        if ( !isset($_SESSION['ipcountry']) ) {
            if ( $ipaddress != '127.0.0.1' ) {
                $xml = @simplexml_load_file('http://freegeoip.net/xml/'.$ipaddress);
                $_SESSION['ipcountry'] = $xml->CountryCode;
            }
            else {
                $_SESSION['ipcountry'] = '--';
            }
        }
        if (!in_array($SESSION['ipcountry'],array('CA','GB','AU','NZ','US')))
        {
            $TEMPLATE->set_message("error", "We can accept registrations only from these countries: United States, UK, Canada, Australia, New Zealand", 0, 1);
            return 0;
        }
    
    PHP:
    seems better for me :)
     
    koko5, May 28, 2012 IP