PHP - Language Detection And Switching
Hello,
I will tell you whant I want to do first and what I've done so far and hopefuly someone will be able to help. What I hope for is to have one unique index.php that will work everywhere. I don't mind if I have to modify each and add path where it is located since this won't happen often. It needs to take car of preferred language and if someone land in a directory, I want it to load the index.php there. I also look to not expose hidden URL in the browser URL box. See *note 1 below. My directory structure is as follow: /index.php /about/index.php /blahblah/index.php The current index.php I'm trying to build looks like this: <?php if (preg_match('/^www.(.+)$/i', $_SERVER['HTTP_HOST'], $matches)) { header("Status: 301 Move permanently", false, 301); header('Location: http://'.$matches[1].$_SERVER['REQUEST_URI']); exit; } session_start(); $Lang = $_SESSION['lang']; if (!isset($Lang)) { if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $Lang = explode(",",$_SERVER['HTTP_ACCEPT_LANGUAGE']); $Lang = strtolower(substr(chop($Lang[0]),0,2)); if ($Lang !== 'fr' && $Lang !== 'en') { $Lang = "en"; } } else { $Lang = "en"; } $_SESSION['lang'] = $Lang; } include $_SERVER["DOCUMENT_ROOT"] . ("/index_" . $_SESSION['lang'] . ".php"); ?> First I remove "www." in the address when applies. Not a must but I like that. Then I check if Language is already set, if not I detect and set it for next pages. I finally load the proper page according to language with an "include" so index_en.php remain secret (sort of). *note 1: all my index_xx.php pages in every directory have this: Code: [Select] <script language="javascript" type="text/javascript"> if (self == top) top.location.href="directory/index.php"; </script> When in the root, it seems to work and I get the proper page but if I place this in /about/ then when I tried to load /about/index.php it doesn't work. I think the include on the last line could be the problem. I'm either close or far from it, I don't know. Thank you. Similar TutorialsCan I rely on the $_SERVER variable of HTTP_ACCEPT_LANGUAGE to get the language of the browser? I know that some browsers don't send things the same way, and even some servers may not use all variables. Is this the most common way to determine the user's language? Is there another or more preferred way?
Thank you.
Hello everyone For a multilingual website we have to be able to open 1 page in 4 different languages. For example, product 'A' is in Dutch, English, French and German. How can I open the right language file? At the moment I use Code: [Select] http://www.zinncosmetics.nl/shop/product_info.php?products_id=36?language=de but it doesn't work. It allways opens the default Dutch pages. What do I do wrong? I don't have very much php experience (newbie) so please don't make it too difficult for me. Thanks for all help! Hi I want to do facial detection (http://corpocrat.com/2009/08/18/automatic-face-detection-with-php-in-linux/), any ways to do it without installing extra stuff (directly, going to try the php.ini add from a php.index) into PHP, as my web host does not allow that. Hi, I would like to be able to receive and emain on what pages users are getting 404 errors (or any other for that fact). I have the directive in my .htaccess to redirect missing pages to my custom 404.php page. However, despite trying a number of tutorial on how to get it to email me the url they tried to access, I can not get it to work. My code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Please Wait</title> <meta http-equiv="refresh" content="0; url=http://www.mysite.co.uk" /> </head> <body> <?PHP $ip = getenv ("REMOTE_ADDR"); $requri = getenv ("REQUEST_URI"); $servname = getenv ("SERVER_NAME"); $combine = $ip . " tried to load " ; $url = $servname . $requri ; $httpref = getenv ("HTTP_REFERER"); $httpagent = getenv ("HTTP_USER_AGENT"); $today = date("D M j Y g:i:s a T"); $message2 = "On $today, $combine:\n http://www.$url\n User Agent = $httpagent \n $httpref "; $to = "my.email@domain.com"; $subject = "404 Error Report"; $from = "From:no-reply@domain.com\r\n"; mail($to, $subject, $message2, $from); ?> </body> </html> I just get: On Mon Jan 16 2012 9:55:57 am UTC, 82.153.239.221 tried to load : http://www.www.mysite.co.uk/404.php User Agent = Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET CLR 2.0.50727; .NET CLR 3.5.30729) I use this script to detect a browser IP, however it doesn't work with IPv6. http://phptutorial.info/iptocountry/the_script.html Any ideas what is required to make it work with both IPv4 & IPv6?
Thanks I have done some searching and digging, but it seems everyone has their own ways of doing things, some more effective than others. I have a web design portfolio website, which uses a "coinslider" flash slideshow. It looks awesome in FF, but IE6/etc users cant view it properly. How can I create a PHP if/then to detect if they are using a sub-par browser, and display a more simple slideshow? That way it will work for all users, and those with Firefox and similar browsers can view the more interesting slideshow. Thanks. Hi all i provided a little code to someone in the misc thread to block users based on the user agent language as an alternative to ipaddresses. I used switch for this but the each line is executed even if my language code is different. Anyone have an idea why the switch code below is messing with me? <?php if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ){ $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ); // $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3'; // need to remove spaces from strings to avoid error $languages = str_replace( ' ', '', $languages ); $languages = explode( ",", $languages ); // do something when you don't like the language switch ($languages[0]) { // first array item case 'ko': // korea echo "do something or set a variable"; break; case 'ru-md'||'ru': // russia echo "do something or set a variable"; break; case 'zh-cn'||'zh-hk'||'zh-mo'||'zh-sg'||'zh-tw'||'zh': // china echo "do something or set a variable"; break; case 'ne': // india echo "do something or set a variable"; break; default: echo "good"; } } ?> I changed it to a different code that uses in_array() which works, but i really would love to know why the above is not working as intended, i looked at php.net, but didn't found something nice in the comments. Maybe a guru out here that knows? <?php if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ){ $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ); // $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3'; // need to remove spaces from strings to avoid error $languages = str_replace( ' ', '', $languages ); $languages = explode( ",", $languages ); // do something when you don't like the language $lang_array = array('ko','ru-md','ru','zh-cn','zh-hk','zh-mo','zh-sg','zh-tw','zh','ne'); if (in_array($languages[0], $lang_array)){ echo 'bad stuff'; }else{ echo 'good stuff'; } } ?> Thanks guys!! Hi I did not se this in any posts and it seems to be a good script. It was supposed to be in somthing I bought but instaed they just put it in a video. Problem is I am getting an error in the if statement, or at least that is what it is telling me. Anyone know what is wrong with it? Appreciate your help in advance Thanks <?php $siteA ="http://www.yoursite.com/A.html"; $siteB ="http://www.yoursite.com/B.html"; $switchtime = "2011-03-14 06:44:00"; $k = strtotime($switchtime); $t = (int)date('U') /* DEBUG */ //echo date("y-m-d h:i:s T",$k); //echo "<br/>".date("Y-m-d h:i:s T",$t); /* */ if($t<$k) header('location: '.$siteA); else header('location: '.$siteB); ?> I'm doing some work with the Discogs API trying to serialize the data of a release for use in another local application. A release on Discogs is something like a music album, so it contains the tracklisting, artists, remixers etc. Here is a sample track row returned from the API (xml) Code: [Select] <track> <artists> <artist> <name>Ramirez</name> <join>And</join> </artist> <artist> <name>Tajada</name> </artist> </artists> <title>Listen To The Drums</title> <duration>8:10</duration> </track> Which more simply looks like this: Ramirez And Tajada - Listen To The Drums On this, I need to class the artist "Tajada" as a guest on this track, however, the "role" is defined in the previous artist's XML row: Code: [Select] <name>Ramirez</name> <join>And</join> How can I sort through the rows to 'swap' these around? Ideally it would look like this: Code: [Select] <track> <artists> <artist> <name>Ramirez</name> </artist> <artist> <name>Tajada</name> <join>And</join> </artist> </artists> <title>Listen To The Drums</title> <duration>8:10</duration> </track> Thanks for any help. Hi guys, I am creating a script as I am using this to detection the proxy server levels. <?php //proxy levels //Level 3 Elite Proxy, connection looks like a regular client //Level 2 Anonymous Proxy, no ip is forworded but target site could still tell it's a proxy //Level 1 Transparent Proxy, ip is forworded and target site would be able to tell it's a proxy if(!$_SERVER['HTTP_X_FORWARDED_FOR'] && !$_SERVER['HTTP_VIA'] && !$_SERVER['HTTP_PROXY_CONNECTION']){ echo '3'; } elseif(!$_SERVER['HTTP_X_FORWARDED_FOR']){ echo '2'; } else echo '1'; ?> I want the script to check the ip that if the proxy server is a Codeen/PlanetLab and BotNet proxy servers, then place on level one and if they are safe/unsafe to use. I cannot find the code to do the methods. Please help me! Thanks in advance. SOLVED Hello Is there any way to find out what is type of proxy? I mean Anonymous, Transparent etc. Here is my simple script for user browser and OS detection: $agent = $_SERVER['HTTP_USER_AGENT']; if (strstr($agent,"Windows")) {$os = "Windows";} if (strstr($agent,"Windows NT 6")) {$os = "Windows 7";} if (strstr($agent,"Linux")) {$os = "Linux";} if (strstr($agent,"Intel Mac OS X")) {$os = "OS X";} if (strstr($agent,"OpenBSD")) {$os = "BSD";} if (strstr($agent,"FreeBSD")) {$os = "FreeBSD";} if (strstr($agent,"Firefox/3")) {$br = "Firefox 3";} if (strstr($agent,"Firefox/2")) {$br = "Firefox 2";} if (strstr($agent,"Firefox/1")) {$br = "Firefox 1";} if (strstr($agent,"Phoenix/")) {$br = "Phoenix (Mozilla lite)";} if (strstr($agent,"MSIE 6")) {$br = "IE 6";} if (strstr($agent,"MSIE 7")) {$br = "IE 7";} if (strstr($agent,"MSIE 8")) {$br = "IE 8";} if (strstr($agent,"Netscape6/6")) {$br = "Netscape 6";} if (strstr($agent,"Opera")) {$br = "Opera";} if (strstr($agent,"Opera") and strstr($agent,"Version/10")) {$br = "Opera 10";} if (strstr($agent,"Opera") and strstr($agent,"Version/9")) {$br = "Opera 9";} if (strstr($agent,"Chrome") and strstr($agent,"Chrome/5")) {$br = "Chrome 5";} if (strstr($agent,"Safari/")) {$br = "Safari";} if (strstr($agent,"Safari/") and strstr($agent,"Version/5")) {$br = "Safari 5";} if (strstr($agent,"Safari/") and strstr($agent,"Version/4")) {$br = "Safari 4";} if (strstr($agent,"Safari/") and strstr($agent,"Version/3")) {$br = "Safari 3";} if (strstr($agent,"SeaMonkey/") and strstr($agent,"Gecko")) {$br = "SeaMonkey";} echo "Browser: <b>$br</b>, OS: <b>$os</b>"; I found some information from he http://www.zytrax.com/tech/web/browser_ids.htm Is there any better solution for this, or script above is accurate? Anyone know of a fairly reliable image nudity detection script? That is open source (PHP or JS)? I was on a proxy and it didnt detect it, idk why. if(!empty($_SERVER['HTTP_X_FORWARDED_FOR']) || !empty($_SERVER['HTTP_X_FORWARDED']) || !empty($_SERVER['HTTP_FORWARDED_FOR']) || empty($_SERVER['HTTP_CONNECTION']) || $hostaddr == "." || stripos($hostaddr, "tor-exit") || !empty($_SERVER['HTTP_VIA']) || empty($_SERVER['HTTP_ACCEPT_ENCODING']) || in_array($_SERVER['REMOTE_PORT'], array(8080,80,6588,8000,3128,553,554)) ) { die('on Proxy'); Hello Has anyone managed to create a php script that will detect and list the AddOns in a browser such as Firefox or Chrome? I need to know if a user has a particular extension loaded and if it is disabled or enabled. Paul Hello all, I need to switch my code from ereg_replace to preg_match but I'm having trouble figuring out how to get the syntax right for this and keep the same functionality. $vendor_ID = ereg_replace("^\*","",$key); Any advice on how to maintain this functionality but use non-deprecated methods? Guys: I'm implementing a form for user submission. At the end of the form, there is an image that displays some randomly generated code. The user will be required to enter the code shown on this image before he clicks the submit button. This is done to protect the form from spamming, automatic submission. After the submit button is clicked, I need to do this: if ($_POST['the code the user enters'] == the code on the image) { // proceed(); } Any idea on how to get the code displayed on the image ? Thanks. Hi guys, Before I am going to create php script to make a proxy detection, I want to ask that if it possible to create a method that checking whether if the proxy server is a located governments or fbi. Is it possible? If so, what method I would use? Thanks, Mark Hi, I came across this code that detects a user's browser and I don't understand parts of it. Here is the code below. Code: [Select] $agent = $_SERVER['HTTP_USER_AGENT']; if ( strpos(strtoupper($agent), 'MSIE')) { print "Internet Explorer"; } else if (strpos(strtoupper($agent), 'FIREFOX')) { print "Firefox"; } else if (strpos(strtoupper($agent), 'KONQUEROR')) { print "Konqueror"; } else if (strpos(strtoupper($agent), "LYNX")) { print "Lynx"; } else { print $agent; } and here is the part I don't understand. Code: [Select] if ( strpos(strtoupper($agent), 'MSIE')) I know that strtoupper makes all letters uppercase, and that strpos returns the position of the string within the larger string, but I don't know why they are in an if statement. Any help or explanation is greatly appreciated. Thanks. Derek |