I know how to detect language browser in PHP and also how to detect the language href anchor for switching to another.
My old codes which doesn't have locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);:
if (isset($_GET['hl']))
{
$langOption = $_GET['hl'];
}
else
{
$langOption = '';
}
switch ($langOption):
case 'ca':
$language = 'cat';
break;
case 'el':
$language = 'el';
break;
case 'en':
$language = 'en';
break;
case 'en_GB':
$language = 'en_GB';
break;
case 'es':
$language = 'es';
break;
case 'fr':
$language = 'fr';
break;
case 'ka':
$language = 'ka';
break;
case 'nl':
$language = 'nl';
break;
case 'pt_PT':
$language = 'pt_PT';
break;
case 'pt_BR':
$language = 'pt_BR';
break;
case 'ro':
$language = 'ro';
break;
default:
$language = 'pt_BR';
break;
endswitch;
require_once("idiomas/{$language}.php");
These old codes worked very for language href anchors. Like:
<a class="waves-effect" href="?hl=en_GB" name="en">English</a>
<a class="waves-effect" href="?hl=pt_BR" name="pt_BR">Português Brasileiro</a>
I changed to codes to condense, economise and shorten the codes. Here are new codes:
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$lang = $_GET['hl'];
}
else
{
$lang = '';
}
switch ($lang):
case $language:
$language = $language;
break;
default:
$language = $language;
break;
endswitch;
require_once("idiomas/{$language}.php");
Only the detection of browser language worked very well, but the language href anchors (hl) do not work, because if you switch to Portuguese, the page still in English.
Similar like:
switch ($lang):
case "en":
$language = "en";
break;
case "en_GB":
$language = "en_GB";
break;
case "pt_BR":
$language = "pt_BR";
break;
case "pt_PT":
$language = "pt_PT";
break;
endswitch;
But I wouldn't like to repeat these old codes. I want to keep condensing and shortening the same new codes.
I don't understand what the switch statement is doing here?
The reason it's not working is because you're not using the $lang variable for anything, it's just setting $language to itself for all cases.
I think this would do what you want :
$language = locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
if (isset($_GET['hl']))
{
$language = $_GET['hl'];
}
As query params can be modified by the user, I'd also suggest using in_array to check that the value is something expected.
I'm building a site in which the user should be able to select from three regions where the content should be visible. I have the following regions:
Region 1: Argentina, Uruguay, Paraguay, Chile, Venezuela, Peru
Region 2: Mexico, Colombia, Ecuador, Brazil, Belize, Costa Rica, El
Salvador, Guatemala, Honduras, Nicaragua, Panama
Region 0: It should include all of the countries above
I have the following switch statement:
switch ( $countryCode ) {
case 'AR':
case 'UY':
case 'PY':
case 'CL':
case 'VE':
case 'PR':
// code to execute
break;
case 'MX':
case 'CO':
case 'EC':
case 'BR':
case 'BZ':
case 'CR':
case 'SV':
case 'GT':
case 'HN':
case 'NI':
case 'PA':
// code to execute
break;
case 'AR':
case 'UY':
case 'PY':
case 'CL':
case 'VE':
case 'PR':
case 'MX':
case 'CO':
case 'EC':
case 'BR':
case 'BZ':
case 'CR':
case 'SV':
case 'GT':
case 'HN':
case 'NI':
case 'PA':
// code to execute
break;
default:
// code to execute
break;
}
I'm having trouble with the structure of my switch code, in the part where I have to include all of the countries (Region 0). It seems that I can't repeat countries that have been declared on previous case statements (it's obvious I know), but I can't figure out the right way to do it.
Is there any way to restructure my code so there would be a case statement that includes all of the countries? If it's not posible to do it with a switch, you guys have any other suggestion?
Thank you!
As #KenWhite mentioned, group by region, then check against regions.
Your code could look something like this:
<?php
$regions = [false, false, false];
switch ( $countryCode ) {
case 'AR':
case 'UY':
case 'PY':
case 'CL':
case 'VE':
case 'PR':
$regions[0] = $regions[1] = true;
break;
case 'MX':
case 'CO':
case 'EC':
case 'BR':
case 'BZ':
case 'CR':
case 'SV':
case 'GT':
case 'HN':
case 'NI':
case 'PA':
$regions[0] = $regions[2] = true;
break;
default:
break;
}
if($regions[0]) {
//some code here
}
// etc.
If I understand the question, you have two mutually exclusive code paths and one inclusive one. I would move away from using switch and use sets (i.e. PHP arrays).
$region1 = array_combine(['AR','UY','PY','CL','VE','PR'],true);
$region2 = array_combine(['MX','CO','EC','BR','BZ','CR','SV',
'GT','HN','NI','PA',],true);
if (isset($region1[$countryCode])) {
// Region 1 specific...
}
else if (isset($region2[$countryCode])) {
// Region 2 specific...
}
if (isset($region1[$countryCode],$region2[$countryCode])) {
// Region 0 specific...
}
else {
// Default case...
}
Better yet (if possible) move the code paths to their own functions:
function region1() { }
function region2() { }
function region0() { }
$region1 = array_combine(['AR','UY','PY','CL','VE','PR'],true);
$region2 = array_combine(['MX','CO','EC','BR','BZ','CR','SV',
'GT','HN','NI','PA',],true);
if (isset($region1[$countryCode])) {
region1();
region0();
}
else if (isset($region2[$countryCode])) {
region2();
region0();
}
else {
// Default case...
}
Options A) hard code your scenario (bad idea)
function get_region($country_code){
switch ( $country_code ) {
case 'AR':
case 'UY':
case 'PY':
case 'CL':
case 'VE':
case 'PR':
return 1;
break;
case 'MX':
case 'CO':
case 'EC':
case 'BR':
case 'BZ':
case 'CR':
case 'SV':
case 'GT':
case 'HN':
case 'NI':
case 'PA':
return 2;
break;
default:
return 0;
break;
}
}
echo get_region("AR")."\n";
echo get_region("MX")."\n";
Option B) use external .ini files
regions.ini
; region 1
[1]
country[] = "AR"
country[] = "UY"
country[] = "PY"
country[] = "CL"
country[] = "VE"
country[] = "PE"
; region 2
[2]
country[] = "MX"
country[] = "CO"
country[] = "EC"
country[] = "BR"
country[] = "BZ"
country[] = "CR"
country[] = "SV"
country[] = "GT"
country[] = "HN"
country[] = "NI"
country[] = "PA"
function.php:
function get_region($country_code){
$region = parse_ini_file("regions.ini", true);
if (in_array($country_code,$region[1]['country'], false)){
return 1;
} elseif (in_array($country_code,$region[2]['country'], false)){
return 2;
} else {
return 0;
}
}
echo get_region("AR")."\n";
echo get_region("MX")."\n";
I'm trying to make a language cookie, but for some reason the cookie isn't saving.
here's the code I'm using
if ( !empty($_GET['language']) ) {
setcookie('language-eclear', $_GET['language']);
}
if ( empty($_COOKIE['language-eclear']) ) {
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
include("langFiles/lang-indexFR.php");
break;
case "de":
include("langFiles/lang-indexDE.php");
break;
case "en":
include("langFiles/lang-indexEN.php");
break;
case "nl":
include("langFiles/lang-indexNL.php");
break;
default:
include("langFiles/lang-indexEN.php");
break;
}
}else{
$lang = $_COOKIE['language-eclear'];
switch ($lang){
case "fr":
include("langFiles/lang-indexFR.php");
break;
case "de":
include("langFiles/lang-indexDE.php");
break;
case "en":
include("langFiles/lang-indexEN.php");
break;
case "nl":
include("langFiles/lang-indexNL.php");
break;
default:
include("langFiles/lang-indexEN.php");
break;
}
}
?>
the if loop for setting the cookie works I tested it by echo'ing $_GET['language'];.
However it seems that the cookie isn't saving. What am I missing?
PS: I'm using a wordpress website
Right now I have all my pages under a switch case for when a visitor uses whatever.php?p=pagename
Here is how it is currently
switch($page)
{
case 'about':
include('pages/about.php');
break;
case 'contact':
include('pages/contact.php');
break;
case 'edb':
include('pages/edb.php');
break;
case 'eluna':
include('pages/eluna.php');
break;
case 'mercsys':
include('pages/mercsys.php');
break;
case 'pastebin':
include('pages/pastebin.php');
break;
case 'projects':
include('pages/projects.php');
break;
case 'sites':
include('pages/sites.php');
break;
case 'soon':
include('pages/soon.php');
break;
case 'sqlgen':
include('pages/sqlgen.php');
break;
case 'wcms':
include('pages/wcms.php');
break;
case 'add':
include('pages/add.php');
break;
case 'edit':
include('pages/edit.php');
break;
case 'delete':
include('pages/delete.php');
break;
case 'moveAnnouncement':
include('pages/moveAnnouncement.php');
break;
default:
include('pages/404.php');
}
My question is, How can I shorten this down to a foreach loop and use the page names for each .php in the pages/ directory without having to add each individual one or any future pages?
upd: like #svrnm adviced, you can do some security checks if you not did it before:
$filename = realpath('pages/' . $page . '.php');
if($filename && file_exists($filename)) {
include($filename);
}
or/and you can build files whitelist first:
$whitelisted = glob("*.php");
if(in_array($page . '.php', $whitelisted) && file_exists($filename)) {
include($filename);
}
I am working on a php base online forum Sincerely speaking i bought the script from codecanyon am still a newbie in php the index page contain $_GET nd switch case which will help in navigating to the other pages but its working keep showing page not find. I have tried all i can pls i need your help thanx.....
`<?php
include("includes/db_config.php");
include("includes/google_config.php");
include("includes/functions.php");
include("includes/loaders.php");
//get web settings
$web = mysql_fetch_array(mysql_query("SELECT * FROM settings ORDER BY id
DESC LIMIT 1"));
//update user online time
if($_SESSION['usern']) {
$user_id = userinfo($_SESSION['usern'],"id");
$online_time = time();
$update = mysql_query("UPDATE users SET online_time='$online_time' WHERE
id='$user_id'");
}
//update forum visits
update_visits();
load_header();
$page = protect($_GET['page']);
}
switch($page) {
case "set_password": include("pages/set_password.php"); break;
case "chat_content": include("pages/chat_content.php"); break;
case "chat": include("pages/chat.php"); break;
case "tag": include("pages/tag.php"); break;
case "forum_sign_in": include("pages/sign_in.php"); break;
case "forum_sign_up": include("pages/sign_up.php"); break;
case "forum_lostpassword": include("pages/lostpassword.php"); break;
case "forum_profile": include("pages/profile.php"); break;
case "forum_messages": include("pages/messages.php"); break;
case "forum_online_users": include("pages/online_users.php"); break;
case "forum_adpanel": include("pages/adpanel.php"); break;
case "view_forum": include("pages/view_forum.php"); break;
case "view_thread": include("pages/view_thread.php"); break;
case "post_thread": include("pages/post_thread.php"); break;
case "post_replie": include("pages/post_replie.php"); break;
case "post_edit": include("pages/post_edit.php"); break;
case "post_delete": include("pages/post_delete.php"); break;
case "post_quote": include("pages/post_quote.php"); break;
case "post_report": include("pages/post_report.php"); break;
case "userinfo": include("pages/userinfo.php"); break;
case "search": include("pages/search.php"); break;
case "read_message": include("pages/read_message.php"); break;
case "send_message": include("pages/send_message.php"); break;
case "reply_message": include("pages/reply_message.php"); break;
case "delete_message": include("pages/delete_message.php"); break;
case "panel": include("pages/panel.php"); break;
case "adpanel_func": include("pages/adpanel_func.php"); break;
case "forum_logout":
unset($_SESSION['usern']);
session_destroy();
session_unset();
$redir = $web['forum_url']."sign_in/";
header("Location: $redir");
break;
default: include("pages/home.php");
}
load_footer();
?>
First you have to debug whats inside the $page variable with:
var_dump($page);
and look whats is the value when you click that link.
NOTE:
I see a brace "`" before the php tag opener, delete it