I'm using custom module to redirect users from Serbia on Serbian language. So, if user comes from Serbia on front page, in URL will be added '/sr'. The problem is that user can't change the language on front page because it always will be redirected.

Can someone help me to fix this issue and make visitor able to change language on front page?

There is the code of custom module:

global $user;
function smart_ip_redirect_init() { 

    // if country is detected
    !empty($_SESSION['smart_ip']['location']['country_code']) && 
    // if is front page and anonymous user
    drupal_is_front_page() && user_is_anonymous()
  ) {
    // load some global variables, language contains information about current language, base_url is base url
    global $language, $base_url;
    // load all active languages
    $languages = language_list();
    // resolve the country code to language, you have to make cases for your needs
    function country_to_language() {
    switch ($_SESSION['smart_ip']['location']['country_code']) {
        case 'RS':
            return 'sr';

            return '';

    // if the URL have no lang parameter
    if(strtoupper(request_uri()) == strtoupper("/")  )
         // does the visitors language exist in our Drupal
      if(array_key_exists(strtolower(country_to_language()), $languages))
        // redirect user to the designated language



Now your code redirect always, and you only want to redirect once. It means you need implement logic similar to the one below:

  1. Test $_SESSION to see if redirection evaluation took place already. If yes, end.

  2. Create element in $_SESSION, to mark this session as already evaluated against redirection

  3. Decide if you want to redirect or not.

  4. Perform redirect if needed.

That way you will run code responsible for redirection once and only once per user session, and that's seems what you want to do.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.