Will this piece of code work


#1

It goes through this code once the user comes into the site from the different countries.

<? if ($COOKIE['visitor_language']== "en") { $lg_id = "UA-1436977-1"; } elseif ($COOKIE['visitor_language']== "fr") { $lg_id = "UA-1436987-1"; } elseif ($COOKIE['visitor_language']== "de") { $lg_id = "UA-1436993-1"; } elseif ($COOKIE['visitor_language']== "nl") { $lg_id = "UA-1436998-1"; } else { $lg_id = "UA-1437004-1"; } ?>

This is where we set the language in the cookie. This is the code for the cookies:

<? /* Script to run at starup */ /* Detecting browser's default language */ if (!isset($_COOKIE['visitor_language'])) { $BrowserLang = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2); switch($BrowserLang) { case "en": $_SESSION['DEFAULT_LANG'] = "en"; break; case "fr": $_SESSION['DEFAULT_LANG'] = "fr"; break; case "de": $_SESSION['DEFAULT_LANG'] = "de"; break; case "nl": $_SESSION['DEFAULT_LANG'] = "nl"; break; case "es": $_SESSION['DEFAULT_LANG'] = "es"; break; } } /* Set Default site language region */ if (!isset($_COOKIE['visitor_language'])) { if (getenv('HTTP_X_FORWARDED_FOR')) { $visitor_ip = getenv('HTTP_X_FORWARDED_FOR'); } else { $visitor_ip = getenv('REMOTE_ADDR'); } $VisitorInfo = file('http://api.hostip.info/get_html.php?ip='. $visitor_ip .'&position=true'); if($VisitorInfo != "") { $VisitorRegion = substr($VisitorInfo[0], -4, 2); switch($VisitorRegion) { case "FR": $_SESSION['DEFAULT_LANG'] = "fr"; break; case "DE": $_SESSION['DEFAULT_LANG'] = "de"; break; case "NL": $_SESSION['DEFAULT_LANG'] = "nl"; break; case "ES": $_SESSION['DEFAULT_LANG'] = "es"; break; } } } /* Register default language in cookie */ $time = time(); if (isset($_COOKIE['visitor_language'])) { $_SESSION['DEFAULT_LANG'] = $_COOKIE['visitor_language']; setcookie ("visitor_language",$_SESSION['DEFAULT_LANG'], $time+(60 * 60 * 24 * 60)); } else { $visitor_lang_pref = $_SESSION['DEFAULT_LANG']; setcookie ("visitor_language",$visitor_lang_pref, $time+(60 * 60 * 24 * 60)); } ?>

Well im testing it the whole time and ran out of ideas of how to make this thing to work, coz my one friend in France is testing and when he goes to the website it brings back the english number $lg_id = “UA-1436977-1” instead of the french number $lg_id = “UA-1436987-1”, so how do i go from here, coz i’ve came to this point where im stuck and feel so stupid(then i ask myself what am i doing in programming). :)


#2

First of all: what are you doing in programming? I certainly hope you’re learning :slight_smile: We all make our first mistake at one point, and most are very simple or dumb to the trained eye. But don’t let that discourage you.

Second, I’d like to request a mod/admin to move this to Beginners or General, as this isn’t a Code Snippet for the general public to use :wink:

Thirdly, might I point out our excellent [php] tagging (now WITH highlighting!)? Works for all your PHP code :smiley:

[php]

<? if ($COOKIE['visitor_language']== "en") { $lg_id = "UA-1436977-1"; } elseif ($COOKIE['visitor_language']== "fr") { $lg_id = "UA-1436987-1"; } elseif ($COOKIE['visitor_language']== "de") { $lg_id = "UA-1436993-1"; } elseif ($COOKIE['visitor_language']== "nl") { $lg_id = "UA-1436998-1"; } else { $lg_id = "UA-1437004-1"; } ?>

[/php]

Okay, this doesn’t seem like it’s very wrong. The only things I’d do differently is use an internal variable for the cookie value and use the full tags:

[php]

<?php $myCookie = $_COOKIE['visitor_language']; if ($myCookie == "en") { $lg_id = "UA-1436977-1"; } elseif ($myCookie == "fr") { $lg_id = "UA-1436987-1"; } elseif ($myCookie == "de") { $lg_id = "UA-1436993-1"; } elseif ($myCookie == "nl") { $lg_id = "UA-1436998-1"; } else { $lg_id = "UA-1437004-1"; } ?>

[/php]

That said, you were forgetting an underscore in your $_COOKIE superglobal :wink: Which may have been the cause for your problem.


#3

THANKS THANKS ONE MILLION THANKS, IT WORKS WITH YOUR CODE.

:lol: :lol:

Your Brilliant


#4

Tell it to my boss :stuck_out_tongue: Anyway, glad I could help :slight_smile: