Jump to content

How to detect visitors country


regoch

Recommended Posts

I got this script for changing language in my page. Now I wonder is there any way to find out from visitor IP from what country is he coming and automatic change language to his language, and if language is not in array to select english.

 

jezik means language in croatian

 

<?php
function pronadiJezik() {

	$jezici = array('hr'=>'Hrvatski', 'en'=>'Engleski', 
	  'de'=>'Njemački', 'it'=>'Talijanski', 'fr'=>'Francuski', 'cz'=>'Češki', 'pl'=>'Poljski');


	if(isset($_SERVER["REDIRECT_URL"]) && $_SERVER["REDIRECT_URL"] != "/") {
		$url = $_SERVER["REDIRECT_URL"];
	} else if(isset($_SERVER["REDIRECT_URI"]) && $_SERVER["REDIRECT_URI"] != "/") {
		$url = $_SERVER["REQUEST_URI"];
	} else {
		$url = "";
	}



	$jeziciA = array_keys($jezici);

	$url_parts = explode("/", $url);


	if(is_array($url_parts)) {

		foreach($url_parts as $n=>$v) {

			if(trim($v)!="") {

				if(in_array($v, $jeziciA)) {

					$jezik = $v;	

				}  

			} // end if $v!=""

		} // end foreach

	} // end if is_array

return $jezik;

}
//----------------------------------------------------------
session_start();
$set_period = (60*60*24);


if($_GET['lang']) {
$_SESSION['jezik'] = $_GET['lang'];
setcookie("jezik", $_GET['lang'], time()+ $set_period); 
} else {
$_SESSION['jezik'] = pronadiJezik();
setcookie("jezik", $_SESSION['lang'], time()+ $set_period); 
} 

if(!$_SESSION['jezik'] or $_SESSION['jezik'] == "") {
$_SESSION['jezik'] = "hr";
setcookie("jezik", "hr", time() + $set_period); 
}
/////////////////////////////////////////////////////////////////////////HR
if($_SESSION['jezik'] == "hr") {
include "include/jezici/hr.php";
}
/////////////////////////////////////////////////////////////////////////EN
elseif($_SESSION['jezik'] == "en") {
include "include/jezici/en.php";
}
/////////////////////////////////////////////////////////////////////////DE
elseif($_SESSION['jezik'] == "de") {
include "include/jezici/de.php";
}
/////////////////////////////////////////////////////////////////////////IT
elseif($_SESSION['jezik'] == "it") {
include "include/jezici/it.php";
}
/////////////////////////////////////////////////////////////////////////FR
elseif($_SESSION['jezik'] == "fr") {
include "include/jezici/fr.php";
}
/////////////////////////////////////////////////////////////////////////CZ
elseif($_SESSION['jezik'] == "cz") {
include "include/jezici/cz.php";
}
/////////////////////////////////////////////////////////////////////////CZ
elseif($_SESSION['jezik'] == "pl") {
include "include/jezici/pl.php";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/250649-how-to-detect-visitors-country/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.