da codice normale a funzione

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
Salve, ho questo codice trovato in internet e funzionante sul mio server web.

PHP:
<?php
/*Get user ip address*/
$ip_address=$_SERVER['REMOTE_ADDR'];

/*Get user ip address details with geoplugin.net*/
$geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
$addrDetailsArr = unserialize(file_get_contents($geopluginURL)); 

/*Get City name by return array*/
$city = $addrDetailsArr['geoplugin_city']; 

/*Get Country name by return array*/
$country = $addrDetailsArr['geoplugin_countryName'];

if(!$city){
   $city='Not Define';
}if(!$country){
   $country='Not Define';
}
echo '<strong>City</strong>:- '.$city.'<br/>';
echo '<strong>Country</strong>:- '.$country.'<br/>';
?>

come faccio tradurlo in una funzione e poi metterlo ad esempio qui:
PHP:
$messaggio = "Nome:&nbsp;&nbsp;". $_POST["nome"]."<br />";
		$messaggio .= "Cognome:&nbsp;&nbsp;". $_POST["cognome"]."<br />";
		$messaggio .= "Indirizzo Ip:&nbsp;&nbsp;". $_SERVER["REMOTE_ADDR"]."<br />";
		$browser = getBrowser($_SERVER['HTTP_USER_AGENT']);
		$messaggio .= "Browser:&nbsp;&nbsp;". $browser."&nbsp;&nbsp;<br />";
		$messaggio .= "Messaggio: <br />". nl2br(replace_text($_POST["messaggio"]))."<br />";
		$messaggio .= "=====================================================================";
		$oggetto = "Richiesta da : ".$_POST["oggetto"]."";
		
		invio_mail_contact($admin_email, $_POST["email"],$oggetto,$messaggio);

lo trovato qui:
http://www.91weblessons.com/get-city-country-by-ip-address-in-php/

idea come fare?

grazie mille e buona giornata.
 
La funzione:

PHP:
<?php
function get_city_country() {
    /*Get user ip address*/
    $ip_address=$_SERVER['REMOTE_ADDR'];

    /*Get user ip address details with geoplugin.net*/
    $geopluginURL='http://www.geoplugin.net/php.gp?ip='.$ip_address;
    $addrDetailsArr = unserialize(file_get_contents($geopluginURL)); 

    /*Get City name by return array*/
    $city = $addrDetailsArr['geoplugin_city']; 

    /*Get Country name by return array*/
    $country = $addrDetailsArr['geoplugin_countryName'];

    if(!$city){
       $city='Not Define';
    }if(!$country){
       $country='Not Define';
    }
    
    $city_country = array('city' => $city, 'country' => $country);
    
    return $city_country;
}
?>


Per usarla:
PHP:
<?php
$city_country = get_city_country();
echo '<strong>City</strong>: ' . $city_country['city'] . '<br>';
echo '<strong>Country</strong>: ' . $city_country['country'] . '<br>';
?>
 

Discussioni simili