2

I have language switcher on my website, it works fine. However, it redirects to base URL.

But when I write redirect($_SERVER["HTTP_REFERER"]); it doesn't redirect correctly. When I change the new language on the home page, I should stay at the same URL and just make the website change the language.

How do I solve this problem?

Here is my controller:

<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class LanguageSwitcher extends CI_Controller
{
    public function __construct() {
        parent::__construct();  
        $this->load->helper('url');    
    }

    function switchLang($language = "") {

        $language = ($language != "") ? $language : "azerbaijani";
        $this->session->set_userdata('site_lang', $language);

        redirect(base_url());

    }
}

Also, I have tried this, it did not work for me:

function switchLang($language = "") {

        if($this->uri->uri_string() == '') {

            $language = ($language != "") ? $language : "azerbaijani";
            $this->session->set_userdata('site_lang', $language);

            redirect(base_url());
        } else {

            $language = ($language != "") ? $language : "azerbaijani";
            $this->session->set_userdata('site_lang', $language);

            redirect($_SERVER["HTTP_REFERER"]);
        }


    }
Vickel
  • 7,879
  • 6
  • 35
  • 56
Javid Abbasov
  • 214
  • 1
  • 4
  • 16
  • have u tried https://stackoverflow.com/a/25651479/8197560 and https://stackoverflow.com/a/21264228/8197560 – nithinTa Nov 30 '17 at 12:00

7 Answers7

1

Try this

$url = $_SERVER['HTTP_REFERER'];
redirect($url);
ashokan
  • 70
  • 5
0

Try using :

redirect($uri, 'refresh');

where $uri is the url of the home page that you want to translate

Sushant Bassi
  • 364
  • 3
  • 16
0

Hope this might help you

$this->load->library('user_agent');
if ($this->agent->is_referral())
{
    echo $this->agent->referrer();
}
Rishi
  • 473
  • 4
  • 13
0
 /*
    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |   http://example.com/
    |
    | WARNING: You MUST set this value!
    |
    | If it is not set, then CodeIgniter will try guess the protocol and path
    | your installation, but due to security concerns the hostname will be set
    | to $_SERVER['SERVER_ADDR'] if available, or localhost otherwise.
    | The auto-detection mechanism exists only for convenience during
    | development and MUST NOT be used in production!
    |
    | If you need to allow multiple domains, remember that this file is still
    | a PHP script and you can easily do that on your own.
    |
    */
    $config['base_url'] = 'your domain';

Try to change the base_url in your config.php inside application/config directory with your URL address complete.

jlosada
  • 46
  • 3
0

What I do in all my projects (maybe not an easy solution for what you're asking for, but realy useful for this and other scenarios) is to use two helpers, I called them history() and back().

History saves current url in an array in session:

$history[] = current_url();
$ci->session->set_userdata('history', $history);

Back($num) receives a number and redirect to that position. i.e: back(1) is equivalent to "send me to the last visited page".

$history = $this->session->userdata('history');
$cant = count($history);
$back = $cant - $num;
redirect($history[$back]);

Then in the __construct() of "CI_Controller" (or whatever class you extend your controllers) call history() once to log every method.

Also, you can add a third controller, I call it no_history().

no_history() deletes the last saved URL (I use it in methods that I don't want to save, for example, ajax requests). You can also check $this->input->is_ajax_request() but it's just an idea.

That's it, now you have a history of visited URLs. You can change whatever you want, and then back(1). Also useful for redirect your users to whatever they were doing before login.

Oh! Almost forgot: Remember to limit the size of the array and always delete the oldest url before adding new one. Between five and ten will be fine.

SomeRSGuy
  • 590
  • 7
  • 19
0

You could do this with sessions, but I prefer this approach which makes use of an uri-query: What it does, is adding a ?my-current-url part to the link where you call the new language.

So, in my view, I've a link (button, etc.) which calls the language switcher, adding the current site url as uri-query like this:

<?php 
  // set pathname from where we came from
     $pn=uri_string();  // the uri class is initialized automatically
?>
<a href="/LanguageSwitcher/switchLang/azerbaijani?<?=$pn?>">Azerbaijani</a>

then in your function switchLang() you take care of the redirect:

function switchLang($language = "") {
    // get pathname from where we came from
    $pn= parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
    $language = ($language != "") ? $language : "azerbaijani";
    $this->session->set_userdata('site_lang', $language);

    // redirect to where we came from
       redirect($pn, 'refresh');    
}
Vickel
  • 7,879
  • 6
  • 35
  • 56
  • it means at the url every time will be `?az` or `?rus` ? – Javid Abbasov Nov 30 '17 at 18:19
  • no, only at the link(button) or whatever you use to click on, there the new language shows. This is obviously based on your quote: "I have language switcher on my website, it works fine" ?$pn will give you the pathname where you came from – Vickel Nov 30 '17 at 18:21
  • First, it redirects to `LanguageSwitcher/switchLang/russian ` page and in a moment redirects to that page where I have changed the language. Is it normal? – Javid Abbasov Nov 30 '17 at 18:36
  • no, the part calls the controller, where you are redirected to the new language page, it should be the same as in your original approach, but instead of redirect(base_url()); now it redirects to the page you came from, but in the new language... BUT WAIT i've set $config['index_page'] = ''; and removed the index part with .htaccess, that could mess up the whole strategy – Vickel Nov 30 '17 at 18:44
  • should I remove it also? – Javid Abbasov Nov 30 '17 at 18:50
  • I would, as it is much nicer looking, also think about using routes, then you can associate an inmyownlanguage-link to your controller/function, you'll find all about routes in the CI manual – Vickel Nov 30 '17 at 18:53
  • Last question, if I don't change .htaccess and route, will there be any problems in future? – Javid Abbasov Nov 30 '17 at 18:59
  • it should work well any way. You said earlier: "it redirects to LanguageSwitcher/switchLang/russian page" but there is no page called in that controller, just a redirect, which works well on my sites Anyway, you can debug your php code quite easy with e.g. adding this line `echo $pn;die();` before your `redirect()` and see what is the outcome. Play around to make the link work as you need. hope this helped.... – Vickel Nov 30 '17 at 19:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160227/discussion-between-vickel-and-javid-abbasov). – Vickel Nov 30 '17 at 19:12
0

First load under constructor method like as:

function __construct()
  {
    parent::__construct();
$this->load->library('user_agent');
}  

After inserting or updating data then it will work fine.

redirect($this->agent->referrer());
Md.Jewel Mia
  • 3,345
  • 3
  • 19
  • 24