0

I've added this code to the index.php:

$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang) {
    case "it":
        session_start();
        $_SESSION['first'] = "si";
        $params = $_SERVER;
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE] 
        = 'italian'; 
        $params[\Magento\Store\Model\StoreManager::PARAM_RUN_TYPE] 
        = 'store'; 
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $params); 
        $app = $bootstrap->createApplication('Magento\Framework\App\Http'); 
        $bootstrap->run($app); 
        break;     
    default: 
        $bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
        $app = $bootstrap->createApplication('Magento\Framework\App\Http');
        $bootstrap->run($app);
    break; 
}

This code detects and redirects properly but whenever I try to switch to English storeview it redirects back to Italian. So, I need something that redirects only on first time page loading.

I've tried to use sessions with object manager but it gives the following error:

Object manager not initialized

  • The code you have posted is badly broken. You have a $ on one line and params on the next. Similarly, you have a couple of -> operators split onto separate lines. Surely, this is not your real code? – Nisse Engström Oct 28 '17 at 18:33
  • It is incorrectly formatted in this question but on php file there aren't mistakes like that, I've asked for adding something to code that allow to redirect only on first time page loading. Anyway thank you for answer. – Roberto Barile Oct 28 '17 at 18:44
  • Thank you for your feedback. You should always (or as far as possible) copy/paste the exact code you're using to avoid confusion. I have suggested an edit to fix the worst formatting problems for you. – Nisse Engström Oct 28 '17 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.