-1

I'm pretty sure this was working before, so I don't know what happened to the code I had in place for a language selector I've been using for a site in development. Here is the php:

<?php
    session_start();
    if(isset($_POST['language'])) {
        $_SESSION['language'] = $_POST['language'];
    }
    $language = (isset($_SESSION['language']) ? $_SESSION['language'] : "english");
    include('/Languages/'.$language.'.php');
?>

The HTML:

<form method="post">
    <input type="image" name="language" value="english" src="Images/english.png"/>
    <input type="image" name="language" value="spanish" src="Images/spanish.png"/>
    ...
</form>

The PHP in each 'dictionary' file:

<?php
    $content = array(
        "identifier1"=>'content with html to be included',
        "identifier2"=>'more content to be included'
    );
?>

And finally, the code on the actual page to call the right language:

<?php echo $content['identifier1']; ?>

So this is supposed to work by having the user click one of the images in the form. The form sends the value of the clicked image, which is the name of the language file that should be opened. The PHP, which is in the head of the page, then takes this value, and includes the dictionary file (eg, english.php), which can then be referred to in the HTML from the echo above.

However, I cannot get this to work. When I click on the image, the page reloads. I've tried doing a <?php echo 'stuff'; ?>, and that works, so my PHP should be working. This means that something must be wrong with the code.

10
  • have you tried to debug at which point the problem occurs? Commented Jan 2, 2014 at 16:24
  • I take it that everything is contained within the same page, since your using <form method="post"> with no action. By default, having no action is like posting to self. Commented Jan 2, 2014 at 16:26
  • 2
    Plus shouldn't <?php echo $content['index1']; ?> be <?php echo $content['identifier1']; ?> ? And your form elements, if they are inputs, may need different names. Both are set to name="language" Commented Jan 2, 2014 at 16:27
  • As shown in the example, $content does not contain an 'index1' entry. Should be 'indentifier1', isn't it? Commented Jan 2, 2014 at 16:28
  • 2
    This code is very dangerous, and allows a user to include ANY php file on your server for which they know the path. Commented Jan 2, 2014 at 16:41

2 Answers 2

2

Okay, apologies for taking so long to reply. It took me a while to do some further research into this, along with other links provided here by others. In any case, I found a better solution which uses cookies instead of a single session as follows:

Before beginning your HTML (or at the very least, before your body tag), add the following PHP code:

<?php
    $lang = "YOUR DEFAULT LANGUAGE";
    $allowedlangs = array(
        "AVAILABLE LANGUAGES 1",
        "AVAILABLE LANGUAGESS 2"
    );
    if (!empty($_GET["lang"])) {
        $rlang = $_GET["lang"];
    } else if (!empty($_COOKIE["lang"])) {
        $rlang = $_COOKIE["lang"];
    }
    if (isset($rlang) && !empty($rlang) && in_array($rlang, $allowedlangs)) {
        $lang = $rlang;
        setcookie("lang", $lang,time()+31536000);
    }
    $langmaps = array(
        "AVAILABLE LANGUAGES 1" => "FILE PATH TO LANGUAGE 1 FILE.php",
        "AVAILABLE LANGUAGES 2" => "FILE PATH TO LANGUAGE 1 FILE.php"
    );
    include($langmaps[$lang]);
?>

So $lang is a variable that initially defines the language you want a new visitor to see. That is, the default language. Whatever value $lang takes, it must be from the array on the next line. It doesn't matter what you call your languages, as long as you can identify them for yourself. The code doesn't care. Call the English language "english", "en", "lang1", "TeaAndCrumpets", or anything you want. Make a new line for each additional language you want to have.

Further down you have the line with $langmaps. The lines in this array will match the names of your languages to the file path that contains your language values. For example, if your English language is "TeaAndCrumpets", the code would be:

"TeaAndCrumpets" => "FILE PATH TO LANGUAGE 1 FILE.php"

Then just type in the file path to the English file, which, again, can be named anything, as long as you point to the right file.

After this, you just need the buttons. No forms needed. Just an for each language you want. In this case, I use images wrapped in tags like so:

<a href="?lang=TeaAndCrumpets"><img src="/images/english.png" title="English" alt="English"></a>
<a href="?lang=vodka"><img src="/images/russian.png" title="Russian" alt="Russian"></a>

This will simply add the URL variable to the address bar, and the PHP kicks in. Make sure whatever value in the href you have for the lang variable matches one of the names in your $allowedlangs array, and that your title and alt attributes make sense to your visitors.

Sign up to request clarification or add additional context in comments.

Comments

0

Ok, a couple of things:

1) Why are you using an associative array for something that is innherently an indexed system? Why not use a regular array?

2) Try debugging. Echo $_SESSION['language'] and make sure the value is even making it through.

Also, try doing a var_dump($content) and see what that gives you after you've included the language file.


Updates:

Add an action to your form. Even if it's empty. That could be having an effect. Did you try a var_dump on your $_POST value?

Also, try

var_dump("/Languages".$language.".php");

However, I would specify the location of your language file via a base directory. Usually in projects I have a global application object store that value as a constant like this:

define("BASEDIR", __DIR__ . "/");

You would, of course, add the appropriate number of "../" or additional filepath information to the end of that if necessary. From there, I use that as the "base directory" for ALL file includes. So, if your language file is at

[base project directory]/Languages/english.php

then you'd include it like so:

include(BASEDIR . "Languages/" . $language . ".php");

Of course, make sure that your base dir constant has a trailing slash, or add one here (I prefer terminating all directory names with a trailing slash).

3 Comments

I lost my edit because someone was editing my post at the same time. The values on my files are the same, but I changed them here to be simpler to read. As for the array thing, no idea. I'm not too familiar with the different arrays, but it worked well enough before. I just don't know why it stopped working all of a sudden.
Yep, it gives me something with the echo: Notice: Undefined index: language in D:\xampp\htdocs\index.php on line 10. So it looks like it had no idea what $_SESSION['language'] is supposed to be.
As a side note, I'd turn on notices for debug purposes if you aren't on a production server. It'll make your life a hell of a lot easier and force you to write much better code!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.