-1

I have a site in three languages:

 www.mysite.com/it/FILES
 www.mysite.com/de/FILES
 www.mysite.com/fr/FILES

On the header of the site I have three link, one for each language. Each link redirect to

/language/index.php

I'd like to redirect the user to the current page he is watching but in the new language.

I tried with

__FILE__

or

dirname

but I didn't reach that!

Thank you!

user2120569
  • 227
  • 1
  • 3
  • 9

1 Answers1

0

Try this in the page your load:

$prefix = $_SERVER['HTTP_HOST'];
$file = explode('/', $_SERVER['PHP_SELF']);
$page = $file[count($file)-1];

if (file_exists($prefix.'/it/'.$page)) {
    http_redirect($prefix.'/it/'.$page); // For Italian
}

if (file_exists($prefix.'/de/'.$page)) {
    http_redirect($prefix.'/de/'.$page); // For German
}

if (file_exists($prefix.'/fr/'.$page)) {
    http_redirect($prefix.'/fr/'.$page); // For French
}

In JavaScript (jQuery click example) you would do something like this:

var host = document.location.hostname;
var path = window.location.href;
var filename = path.replace(/^.*[\\\/]/, '');

$('.myItalianLink').click(function(){
    window.location.href = host + '/it/' + filename; // For Italian
});

console.log(host + '/it/' + filename);

Outputs:

http://www.mywebsite.com/it/html4.php

The JavaScript approach will likely be more suiting for your needs, as it's more reactive in case of user interaction (the links you named).

  • so you suggest me to you jquery? With the php solution, I have a problem: the redirect is always to /it/it/page or /de/de/page or /fr/fr/page because the language is include in $_SERVER['SCRIPT_NAME'] – user2120569 Mar 24 '13 at 23:46
  • Not per se, you can just use a normal JavaScript onClick too. But you are expecting user input. It's nicer to do that client-side. You could do a new PHP request + a redirect too, but in your case it's a usability question, and does not incur security risk. –  Mar 24 '13 at 23:48
  • @user2120569 my bad; I've updated the code to use `$page = $_SERVER['PHP_SELF'];`. This code will give you all PHP's server variables: `echo '
    '; print_r($_COOKIE); print_r($_ENV); print_r($_FILES); print_r($_GET); print_r($_POST); print_r($_REQUEST); print_r($_SERVER); print_r($_SESSION); echo '
    ';` (`$_SESSION` can be non-existent and some are empty when not used, like `$_POST`).
    –  Mar 24 '13 at 23:49
  • with $_SERVER['PHP_SELF'] is the same. I think I'll use something like a function($lang) with str_replace and simply replace on the current url /it/ with /de/ or /fr/ and vice versa. – user2120569 Mar 24 '13 at 23:57
  • I've updated the answer. It should work now. I would recommend you use JavaScript for this situation tho. –  Mar 25 '13 at 00:05
  • I deleted one host, otherwise it was redirecting two times at the web site. var host = document.location.hostname; var path = window.location.href; var filename = path.replace(/^.*[\\\/]/, ''); $('#ted').click(function(){ window.location.href = '/de/' + filename; // For German }); console.log('/de/' + filename); – user2120569 Mar 25 '13 at 00:19