jartaud
jartaud

Reputation: 385

Help on changing the page language without changing the current page (PHP)

langauage web. I set 3 links, Français... (href=changeLanguage.php?lang=fr,es,en)

changLanguage.php

session_start();

if(isset($_SESSION['bckTo']) && isset($_GET['lang'])){

    $lang = preg_replace('/[^a-z]/','',trim($_GET['lang']));

    #TODO
    #More vlidation ...

    $full_url = $_SESSION['bckTo']; 
    $full_url = str_replace(array('&lang=en','&lang=es','&lang=fr'),'',$full_url);

header('Location: '.$full_url.'&lang='.$lang.'');
}

$_SESSION['bckTo'] save the current URL for example: mysite.com/index.php?id=x&n_d=y The problem is, the header translate the URL to: mysite.com/index.php?id=x&n_d=y&lang=en.

Any idea will be appreciate

Upvotes: 2

Views: 1980

Answers (2)

erjiang
erjiang

Reputation: 45657

Running html_entity_decode will convert those HTML entities back into ampersands.

https://www.php.net/manual/en/function.html-entity-decode.php

Upvotes: 1

Jim
Jim

Reputation: 18853

Why not just set the language in session instead of via GET? Then you just have to put a link to change the language and and then redirect to the page. This would probably be best, given that you are already using sessions.

Example: <a href="changeLanguage.php?lang=en">English</a>

On the changeLanguage:

//code up here
if (isset($_SESSION['bckTo') && isset($_GET['lang'])) {
     // $lang code here
     $_SESSION['lang'] = $lang;
     header('Location: ' . $_SESSION['bckTo']);
}

Then you would just need to change your language checking / displaying code to check the session variable rather than the GET (on the actual pages).

Upvotes: 4

Related Questions