B
B
Baffer2019-03-03 10:44:19
PHP
Baffer, 2019-03-03 10:44:19

The session is not saved, the reason?

The fact is that there is a language switcher

//session_start();
header('Cache-control: private'); // IE 6 FIX

if(isset($_GET['lang'])) {
    
    $lang = $_GET['lang'];

    // Register the session and set the cookie.
    $_SESSION['lang'] = $lang;

    setcookie("lang", $lang, time() + (3600 * 24 * 30));
    
    } else if(isSet($_SESSION['lang'])) {
    
    $lang = $_SESSION['lang'];
    
    } else if(isSet($_COOKIE['lang'])) {
    
    $lang = $_COOKIE['lang'];
    
    } else {
    
    $lang = 'en';
    
    }

switch ($lang) {
  case 'en':
  $lang_file = 'english.php';
  break;
        
  case 'ru':
  $lang_file = 'russian.php';
  break;
    
  default:
  $lang_file = 'english.php';

}

include_once 'languages/'.$lang_file;

There is a language switch, it works, but after reloading the page or switching to another page, the language is reset to the standard one.
At the very top there is //session_start(); , the fact is that there is already a session in init.php (root file) when called in this file (lang.php), an error occurs that Headers have been sent .. but with this error the language is not reset .. but it works fine.
the switch itself
<a href="lang/en" class="footer-link pt-3">English</a>
 <a href="lang/ru" class="footer-link pt-3 ml-4">Russian</a>

path in .htaccess
RewriteRule ^lang/(.*)?$ index.php?router=feed〈=$1 [L]

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitsliputsli, 2019-03-03
@Baffer

Why is this:
RewriteRule ^lang/(.*)?$ index.php?router=feed〈=$1 [L]
?
As a result, you send a request:
server.com/lang/ru
Apache transforms the request, and in php there will be a GET parameter:
$_GET['router'] = 'feed〈=en'
and in the script you check $_GET['lang '].
It may be easier to remove the rule in Apache (you may not delete it) and change the switch to:
href="?lang=en", because in this form, your handler expects data.

E
Egor, 2019-03-03
@egormmm

Remove header from this file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question