0

I am trying to look up the visitor's location and show this on the product page. It's going to be used to send them a personalised message based on location but currently, I am struggling to get it to show the current state.

If I put the code in a normal PHP file and load this it works fine but when I try this on a custom block in a .phthm file I get an error loading the product page saying undefined variable.

I can pull product details but not sure how I correctly define and show this on a product page.

Exception #0 (Exception): Notice: Undefined variable: ip in /home/staging/public_html/app/code/drumbeat/tabmodule/view/frontend/templates/shipping2.phtml on line 76

<?php 
function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
 $output = NULL;
 $state=NULL;
 $ip=NULL;
 global $ip;
 global $state;
 if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
 $ip = $_SERVER["REMOTE_ADDR"];
 if ($deep_detect) {
 if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
 if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
 $ip = $_SERVER['HTTP_CLIENT_IP'];
 }
 }
 $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
 $support = array("country", "countrycode", "state", "region", "city", "location", "address");
 $continents = array(
 "AF" => "Africa",
 "AN" => "Antarctica",
 "AS" => "Asia",
 "EU" => "Europe",
 "OC" => "Australia (Oceania)",
 "NA" => "North America",
 "SA" => "South America"
 );
 if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
 $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
 if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
 switch ($purpose) {
 case "location":
 $output = array(
 "city" => @$ipdat->geoplugin_city,
 "state" => @$ipdat->geoplugin_regionName,
 "country" => @$ipdat->geoplugin_countryName,
 "country_code" => @$ipdat->geoplugin_countryCode,
 "continent" => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
 "continent_code" => @$ipdat->geoplugin_continentCode
 );
 break;
 case "address":
 $address = array($ipdat->geoplugin_countryName);
 if (@strlen($ipdat->geoplugin_regionName) >= 1)
 $address[] = $ipdat->geoplugin_regionName;
 if (@strlen($ipdat->geoplugin_city) >= 1)
 $address[] = $ipdat->geoplugin_city;
 $output = implode(", ", array_reverse($address));
 break;
 case "city":
 $output = @$ipdat->geoplugin_city;
 break;
 case "state":
 $output = @$ipdat->geoplugin_regionName;
 break;
 case "region":
 $output = @$ipdat->geoplugin_regionName;
 break;
 case "country":
 $output = @$ipdat->geoplugin_countryName;
 break;
 case "countrycode":
 $output = @$ipdat->geoplugin_countryCode;
 break;
 }
 }
 }
 // return $output;
}
?>
<div>
 <p>Hello</p>
 <p><?php echo $ip('State'); ?></p>
 </div>
Rama Chandran M
3,26515 gold badges24 silver badges38 bronze badges
asked Aug 14, 2018 at 21:56

1 Answer 1

0

Instead of this method I used jQuery Instead as below,

In case helps anyone:

<script>
jQuery(document).ready(function($) {
$.get("https://ipinfo.io/json", function (response) {
 $("#ip").append("Your IP is: " + response.ip);
 var regionChk = response.region;
 switch (regionChk)
 {
 case "England":
 $("#region").append("100% FREE 1-Day Delivery " + response.region);
 break;
 case "Wyoming":
 $("#region").append("100% FREE 2-Day Delivery ");
 break;
 default:
 $("#region").append('100% FREE 3-Day US Shipping' + response.region);
 }
 }, "jsonp");
});
</script>
Rama Chandran M
3,26515 gold badges24 silver badges38 bronze badges
answered Aug 16, 2018 at 10:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.