Multi-lang website: Is this a smart way of doing it? - php

function title_of_page() {
// Get the user's location
$user_location = $_SERVER['REQUEST_URI'];
//Find just the filename
$filename = pathinfo($user_location, PATHINFO_FILENAME);
if ($user_location == "") {
switch ($filename) {
case 'intro': echo 'introduction'; break;
case 'about': echo 'about the author'; break;
case 'foreword': echo 'foreword'; break;
case 'pottery': echo 'pottery'; break;
case 'gold_jewelery': echo 'jewelry in bahrain'; break;
case 'metalwork': echo 'metalwork'; break;
case 'weaving': echo 'weaving'; break;
case 'glass': echo 'glass industry'; break;
case 'woodwork': echo 'woodwork'; break;
case 'plant': echo 'Plant-product handicrafts'; break;
case 'calligraphy': echo 'arabic calligraphy'; break;
case 'thanks': echo 'thanks & appreciation'; break;
case 'craft': echo 'craft centres'; break;
case 'videos': echo 'videos'; break;
case 'pano': echo 'panoramic'; break;
// These are the crafts subpages
case 'jasra_handicrafts': echo 'Al Jasra Handicrafts Center'; break;
case 'jasra_training': echo 'Al Jasra Training Center'; break;
case 'capital_mall': echo 'Capital Mall'; break;
case 'craft_industries': echo 'The Craft Industries Development Centre'; break;
default: echo '<img id="logo" src="img/logo.png">'; break;}
} else {
// ARABIC TITLES
switch ($user_location) {
case '/ar/intro.php': echo 'مقدمة'; break;
case '/ar/about.php': echo 'نبذة عن الكاتب'; break;
case '/ar/foreword.php.php': echo 'تقديم'; break;
case '/ar/pottery.php': echo 'الفخار'; break;
case '/ar/gold_jewelery.php': echo 'حلي الذهب والفضة واللآلئ'; break;
case '/ar/metalwork.php': echo 'الأعمال المعدنية'; break;
case '/ar/weaving.php': echo 'النسيج والتطريز وتفصيل الملابس'; break;
case '/ar/glass.php': echo 'صناعة الزجاج'; break;
case '/ar/woodwork.php': echo 'الأشغال الخشبية'; break;
case '/ar/plant.php': echo 'صناعة المنتجات من المواد المأخوذة من النباتات'; break;
case '/ar/calligraphy.php': echo 'الخط العربي'; break;
case '/ar/thanks.php': echo 'شكر وتقدير'; break;
case '/ar/craft.php': echo 'المراكز الحرفية'; break;
case '/ar/panoramic.php': echo 'بانوراما'; break;
// These are the crafts subpages
case '/ar/jasra_handicrafts.php': echo 'مركز الجسرة للحرف اليدوية'; break;
case '/ar/jasra_training.php': echo 'مركز الجسرة للتدريب'; break;
case '/ar/capital_mall.php': echo 'بمجمع العاصمة'; break;
case '/ar/craft_industries.php': echo 'مركز تنمية الصناعات الحرفية'; break;
default: echo '<img id="logo" src="../img/logo.png">'; break;}
}
}
This is currently the code i'm using. I'm building a PhoneGap app for a client of mine and the app is stationery (as in, client doesn't want a CMS). Anyway, what I want to happen is that when the user is viewing the English side of the app, it'll show the English titles however when the user is on the Arabic side of the app it'll show the same titles in Arabic.
Just wondering if i'm going the right way..?

Related

Multiple Switch Statements

Okay, I have one PHP file which needs to contain several switch($_GET['']) statements. For example: switch($_GET['id']), switch($_GET['open']), switch($_GET['number'])... Do I have to close it like:
switch($_GET['id'])
{
}
Or:
switch($_GET['open'])
{
};
One below another with or without semicolon?
This is my index.php:
It does not fully work. My php file is like this (Index.php):
<?php
// THE MAIN SITE
switch($_GET['open'])
{
default: include("Home-Page.php");
case 'Site': include("LetsStart/Pages/Home.php"); break;
case 'Links: switch ($_GET['topics'])
{
default: include("LetsStart/Pages/Links.php"); break;
case 'Tourism': include("LetsStart/Pages/Tourism.php"); break;
case 'Finance': include("LetsStart/Pages/Finance.php"); break;
case 'Health Care': include("LetsStart/Pages/HealthCare.php"); break;
}
break;
case 'About Us': switch ($_GET['details'])
{
default: include("LetsStart/Pages/AboutUs.php"); break;
case 'What We Do': include("LetsStart/Pages/WWD.php"); break;
case 'Our History': include("LetsStart/Pages/OurHistory.php"); break;
}
break;
}
// ENCYCLOPEDIA
switch($_GET['letter'])
{
case 'B': switch($_GET['term'])
{
default: include("LetsStart/Pages/TheEncyclopedia/Letter-B-Main.php"); break;
case 'Term 1': include("LetsStart/Pages/TheEncyclopedia/B/1.php"); break;
case 'Term 2': include("LetsStart/Pages/TheEncyclopedia/B/2.php"); break;
case 'Term 2': include("LetsStart/Pages/TheEncyclopedia/B/3.php"); break;
}
break;
}
?>
It keeps loading my home page and the first page from the second switch.
You do not need a semicolon after the closing bracket of a switch statement (same as an if statement).
You don't need semicolon, because the $_GET['id'] is a variable, not a string. Read this http://www.w3schools.com/php/php_switch.asp

PHP - Simple Cookie Not Saving (Wordpress)

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

PHP Getting all file names in dirctory for page switching

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);
}

PHP . $_GET and switch case to navigate url from one page to another

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

Can I have multiple cases that do the same thing?

The first one is definitely something that works, but which one below is the efficient way?
switch($type) {
case 1:
print 'success';
break;
case 2:
print 'success';
break;
case 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
Since 1, 2 and 3 print do the same, can I do this?
switch($type) {
case 1, 2, 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
or
switch($type) {
case 1:
case 2:
case 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
switch($type)
{
case 1:
case 2:
case 3:
print 'success';
break;
case 4:
print 'success for type 4';
break;
}
Is the way to go!
PHP manual lists an example like your 3rd for switch:
<?php
switch ($i) {
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>
I agree with the others on the usage of:
switch ($i) {
case 0: //drop
case 1: //drop
case 2: //drop
echo "i is 0, 1, or 2";
break;
// or you can line them up like this.
case 3: case 4: case 5:
echo "i is 3, 4 or 5";
break;
}
The only thing I would add is the comments for the multi-line drop through case statements, so that way you know it's not a bug when you (or someone else) looks at the code after it was initially written.

Categories