0

I'm building a small static website in HTML and PHP. The website include two languages, Spanish and English. Spanish is the language set by default.

I think I have an issue with the PHP code. When I click the links to switch languages it doesn't work. You can easily reproduce the issue with the following code divided in 4 different PHP files.

index.php

<!doctype html>
<html class="no-js" lang="es">
<head>
  <meta charset="utf-8">
</head>
<body>

<?php
if (empty($_SESSION["langfile"])) { $_SESSION["langfile"] = "spanish.php"; }
require_once ($_SESSION["langfile"]);
echo $language["hi"];
?>

<a href="language.php?l=english">En</a>
<a href="language.php?l=spanish">Es</a>

</body>
</html>

language.php

<?php
if (!empty($_GET["l"]))
{
$_SESSION["langfile"] = $_GET["l"] . ".php";
header ("location: index.php"); exit();
}
?>

spanish.php

<?php $language["hi"] = 'Hola'; ?>

english.php

<?php $language["hi"] = 'Hello'; ?>

Any help would be much appreciated, thank you!

Mathieu Préaud
  • 357
  • 3
  • 16
  • 2
    I don't see a `session_start()` command anywhere. You need that at the beginning of every script where you wish to use the Session. – ADyson Jun 01 '21 at 15:56
  • Thank you @ADyson. I'm new in PHP so I'm not sure how to implement it. Do you have any example? – Mathieu Préaud Jun 01 '21 at 15:58
  • Read the last sentence of my comment again - I already told you exactly where to put it. it's just a line of code, you just write it like any other. Also, take any PHP tutorial about sessions and it would show you. – ADyson Jun 01 '21 at 15:59
  • 2
    ADyson was first ;) Anyway that's obvious that you need to start your session. No example is required in this case, just [read the doc](https://www.php.net/manual/en/function.session-start.php) (and samples placed there) – biesior Jun 01 '21 at 16:01
  • Thank you guys, it works! – Mathieu Préaud Jun 01 '21 at 16:27

0 Answers0