0

1st problem

I have a dynamic site where the pages are included in the index page dynamically in the following way:

 $page = (empty($_GET["page"])) ? "home" : $_GET["page"];
 $page = "folder/".basename($page).".php";

 if (is_readable($page)) { 
 include($page); 
 } else {
   echo 'Site doesn't exist!'; 
 exit;
 }

But there is also a language menu which changes the languages with another $_GET parameter:

<form name="switch" id="lang" action="" method="get">
<select name="multilingual" onchange="this.form.submit()">
<option value="lang_it">Italian</option>
<option value="lang_de" >German</option>
<option value="lang_en">English</option>
</select>
</form>

if(!isset ($_COOKIE['multilingual'])){
$lingua = null; 
}else{ 
$lingua = $_COOKIE['multilingual'];
}

if(isset($_GET['multilingual'])){
$lingua = $_GET['multilingual'];
$_SESSION['multilingual'] = $lingua;
setcookie('multilingual', $lingua, time() + (3600 * 24 * 30));
}
switch($lingua){
case 'lang_it': include_once('file_it.php');  break;
case 'lang_de': include_once('file_de.php');  break;
case 'lang_en': include_once('file_en.php');  break;
default: include_once('file_it.php'); break;
}

The second $_GET parameter is passed to the URL but on the pages it will redirect me always to the default home page.

How to change the language without beiing redirected to the home page? In other words, ho to change the language and stay on the same page?

2nd problem

Is it possibile to change the lang_it, lang_de, lang_en parameter in the URL to get redirected to the current language of the page. If this will happen, how to change the language switcher when the page changes to another language?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Someone33
  • 568
  • 2
  • 8
  • 25
  • Check out this answer: http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a – larsAnders Feb 27 '14 at 14:52
  • Thanks for the hint, but it's not solving my problems. Also if i'm writing REQUEST_URI in the action field. – Someone33 Feb 27 '14 at 14:59

2 Answers2

1
<form name="switch" id="lang" action="<?php echo $page; ?>" method="get">
<select name="multilingual" onchange="this.form.submit()">
<option value="lang_it" <?php if($_COOKIE['multilingual']=='lang_it') echo "selected='selected'" ?>>Italian</option>
<option value="lang_de" <?php if($_COOKIE['multilingual']=='lang_de') echo "selected='selected'" ?>>German</option>
<option value="lang_en"<?php if($_COOKIE['multilingual']=='lang_en') echo "selected='selected'" ?>>English</option>
</select>
</form>
nowhere
  • 1,558
  • 1
  • 12
  • 31
  • Still beeing redirected to the index page. :-( Also, the language switcher changes only the second time, but the language obviously is changing. I thing it's not working because the request_uri is always the index page, and not the other included pages – Someone33 Feb 27 '14 at 14:55
  • try adding $_GET["page"]; in the action. does it do the trick. What do you mean with the language switcher changes only the second time? – nowhere Feb 27 '14 at 15:18
  • Now i get an undefined error for the global $_GET['page'] because the switching menu is included. But why? I thought $_GET is an global variable? – Someone33 Feb 27 '14 at 15:20
  • mmm what if you use your variable $page instead of $_GET['page'] – nowhere Feb 27 '14 at 15:25
  • For the language switcher: I must select the option twice to get the language displayed. But the language is changing, it's only a problem of the switcher. Where should i write the variable $page? Thanks a lot for your help! – Someone33 Feb 27 '14 at 15:30
  • That's not possible, because the pages are included in the index.php. If i make a direct request to $page, i will only get the single pages, without all the stuff in the index (scripts, css, footer, include files, etc.) – Someone33 Feb 27 '14 at 15:57
  • I think the problem is also in the index code, nut i don't know how to modify it (e.g.: include the GET value from the language switcher in the index code): `$page = (empty($_GET["page"])) ? "home" : $_GET["page"];` **EDIT:** With the POST method in the switcher it's working perfectly. The only problem is that the URL remains the same for all languages, and it's very bad for indexing. This is the reason why i'm trying to use GET in the switcher. – Someone33 Feb 27 '14 at 16:04
  • Your answer was not solving my problems, but sent me in the right direction. Thanks! – Someone33 Feb 28 '14 at 16:01
0

this.form.submit() not gonna work without page refresh/redirect.

If i understood your problem correctly:

You need to start using one-page JS frameworks

or load language vars dynamically in the background via (again) JS (load laguage vars via json config populate/change/mutate DOM contents)

EDIT

Forgot to mention: be careful with your "dynamic" website. The way you do it is quite unsafe, security wise.

EDIT Try changing forms action attibute. Put there current filename

EDIT More secure way is to have an array with pages that are actually exist and safe and after form submit check $_GET parameters against that array

rinchik
  • 2,642
  • 8
  • 29
  • 46
  • My code works perfectly if i use $_POST instead of $_GET in the language switcher. Js for changing language is not good for the SEO. The URL's should be unique. That's the reason why i'm trying to use $_GET in the language switcher. Can you tell me what's not secure in the code? – Someone33 Feb 27 '14 at 15:40
  • Already tried it with REQUEST_URI in the "action". Still no luck. Maybe some more specified answer, some code? Thanks in advance! – Someone33 Feb 27 '14 at 15:46