简体   繁体   中英

Changing the path in a URL for a multilingual website

I am coming to you to find out if it is possible to change the path in a URL using a PHP variable. My goal would be to organize my website with 2 folders (one for each language), choose the language on a random page in one of the two folders and be able to change the language by modifying the URL.

For example, I'm on http://truc.com/fr/test.php ; I change the language and I get http://truc.com/en/test.php .

I've done a test that works but I'd prefer something more dynamic, that will work on all the pages of the website: fichier index.php

<?php
session_start();
if (!isset($_SESSION['lang']))
    if (!isset($_SESSION['lang']))
    $_SESSION['lang'] = "fr";
else if (isset($_GET['lang']) && $_SESSION['lang'] != $_GET['lang'] && !empty($_GET['lang'])) 
    {
        if ($_GET['lang'] == "fr")
            $_SESSION['lang'] = "fr";
        else if ($_GET['lang'] == "en")
            $_SESSION['lang'] = "en";
    }
if($_SESSION['lang']==fr)
    {
        header('Location: http://localhost/site/languages/fr/index.php');
    }
else if($_SESSION['lang']==en)
    {
        header('Location: http://localhost/site/languages/en/index.php');
    }
?>

Adjusted your code and added a new variable $page_language that handles the page language and redirects to specific link for various languages and before redirecting to the page a session language is set to remember it on next page . Good luck

if (isset($_SESSION['lang'])) {
  $page_language = $_SESSION['lang'];
} elseif (isset($_GET['lang']) && !empty($_GET['lang'])) {
  $page_language = $_GET['lang'] ;
} else {
  $page_language = "fr" ; //set default language if no session and url has language
}

if ($page_language == "fr") {
  $_SESSION['lang'] = "fr"; //set session language for later visits
  header('Location: http://localhost/site/languages/fr/index.php');
  exit;
} elseif ($page_language == "en") {
  $_SESSION['lang'] = "en"; //set session language for later visits
  header('Location: http://localhost/site/languages/fr/index.php');
  exit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM