D
D
Denis2016-06-02 14:21:31
PHP
Denis, 2016-06-02 14:21:31

How to make an automatic redirect to the selected language?

Hello everyone!)
Recently I decided to make the site two languages ​​- Russian and English. I have different pages for each language. My Russian language is just on the example.com domain , and English on example.com/en/ . And I want to make sure that when the user enters and selects English, when entering example.com, he (the user) is automatically redirected to example.com/en/
How to do this? Thanks in advance! :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lumore, 2016-06-02
@Denpin

<a href="/lang.php?lang=en">En</a>

lang.php:
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
header('Location: /'.$lang);

index.php:
if(!isset($_SESSION['lang']) {
$_SESSION['lang'] = 'ru';
}
if(isset($_SESSION['lang']) && $_SESSION['lang'] == 'en') {
header('Location: /en');
}

Written by hand, there may be errors.

A
Alex, 2016-06-02
@streetflush

I store the language in cookies and use the cookies to determine which page to go to.
Yes, and the country of the user can be determined without a choice ... I asked here somehow How to determine the country by IP in PHP?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question