Creating a PHP function -
so i'm trying create php function track visitors. reason why want function of because not want copy/paste code every page i'll using tracker. must confess php functions weakest. appreciated.
this current function:
function visitor_ip_data($ip_city) { if(!isset($_session['ip_country']) || !isset($_session['ip_city']) || !isset($_session['ip_postal']) || !isset($_session['ip_region']) || !isset($_session['ip_isp']) ) { //niet geset, haal nieuwe ip data op $url = file_get_contents('http://api.ipaddresslabs.com/iplocation/v1.8/locateip?key=my-key-goes-here&ip='.$_server['remote_addr'].'&format=json'); $html = json_decode($url); if ($html->query_status->query_status_code != 'ok') { //api data error //destroy sessions unset($_session['ip_city']); unset($_session['ip_postal']); unset($_session['ip_region']); unset($_session['ip_country']); unset($_session['ip_isp']); //echo "error! ".$html->query_status->query_status_description." "; //die(); } else { //api data success //create sessions $feed = $html->geolocation_data; $_session['ip_city'] = $feed->city; $_session['ip_postal'] = $feed->postal_code; $_session['ip_region'] = $feed->region_name; $_session['ip_country'] = $feed->country_name; $_session['ip_isp'] = $feed->isp; $ip_city = $_session['ip_city']; $ip_postal = $_session['ip_postal']; $ip_region = $_session['ip_region']; $ip_country = $_session['ip_country']; $ip_isp = $_session['ip_isp']; //echo "gathered ip data api"; //die(); } } else { //ip data in sessions //use sessions $ip_city = $_session['ip_city']; $ip_postal = $_session['ip_postal']; $ip_region = $_session['ip_region']; $ip_country = $_session['ip_country']; $ip_isp = $_session['ip_isp']; //echo "using ip data sessions"; //die(); } }
what i'm trying echoĆng $ip_city on page using function. part of page code following:
echo visitor_ip_data($ip_city);
but gives me undefined variable error message. i'm wondering why it's doing that. missing or doing wrong?
thank you.
you don't return function.
remove argument function definition : function visitor_ip_data()
write
//echo "error! ".$html->query_status->query_status_description." "; return null; } else { //api data success //create sessions $feed = $html->geolocation_data; $_session['ip_city'] = $feed->city; $_session['ip_postal'] = $feed->postal_code; $_session['ip_region'] = $feed->region_name; $_session['ip_country'] = $feed->country_name; $_session['ip_isp'] = $feed->isp; $data['ip_city'] = = $_session['ip_city']; $data['ip_postal'] = $_session['ip_postal']; $data['ip_region'] = $_session['ip_region']; $data['ip_country'] = $_session['ip_country']; $data['ip_isp'] = $_session['ip_isp']; //echo "gathered ip data api"; //die(); } } else { //ip data in sessions //use sessions $data['ip_city'] = $_session['ip_city']; $data['ip_postal'] = $_session['ip_postal']; $data['ip_region'] = $_session['ip_region']; $data['ip_country'] = $_session['ip_country']; $data['ip_isp'] = $_session['ip_isp']; //echo "using ip data sessions"; //die(); } return $data;
and then
if (($data = visitor_ip_data()) == null) echo 'error'; else echo $data['ip_city'];
Comments
Post a Comment