Jump to content

Recommended Posts

Where should i set setcookie() to remember what language is selected?

 

includes/configs/a_panel/index.php

<?php
/* ---------------------------------- */
require ROOT_DIR . 'includes/language_changer/a_panel/index.php';

if (is_file(ROOT_DIR . 'languages/a_panel/'.Language::get_language($db, $config).'/main/index.php') && file_exists(ROOT_DIR . 'languages/a_panel/'.Language::get_language($db, $config).'/main/index.php')) {
    $language = Language::get_language($db, $config);
    $charset = Language::get_charset($db, $language);
    require_once(ROOT_DIR . 'languages/a_panel/'.$language.'/main/index.php');
}

else {
    $language = 'en';
    $charset = 'utf-8';
    require_once(ROOT_DIR . 'languages/a_panel/'.$language.'/main/index.php');
}
/* ---------------------------------- */

$smarty->assign('db', $db);
$smarty->assign('config', $config);
$smarty->assign('ui', $ui);
$smarty->assign('lang', $lang);
$smarty->assign('language', $language);
$smarty->assign('charset', $charset);
?>

includes/language_changer/a_panel/index.php

<?php

class Language {
	
    public static function get_language($db, $config) {
		
	if ($config['ap_allow_language_changing'] === 'true') {
	
	    if (isset($_GET['language']) && self::is_language_supported($db, $config, $_GET['language'])) {
	        return $_GET['language'];
	    }
	
	    else if (isset($_COOKIE['admin_language']) && self::is_language_supported($db, $config, $_COOKIE['admin_language'])) {
		return $_COOKIE['admin_language'];
	    }
		
	}
	
	return $config['ap_default_language'];
		
    }
	
    private static function is_language_supported($db, $config, $language) {
		
	$query = 'SELECT folder_name AS lang_code FROM language WHERE panel = "a_panel" AND version = :version';
	$select = $db->prepare($query);
	$select->bindParam(':version', $config['script_version'], PDO::PARAM_STR);
	$select->execute();
	
	$supported_languages = array();
	while($row = $select->fetch(PDO::FETCH_ASSOC)){
   	    $supported_languages[] = $row['lang_code'];
        }
	
	if (in_array($language, $supported_languages)) {
	    return true;
	}
	
	return false;
		
    }
	
    public static function get_charset($db, $language) {
		
	$query = 'SELECT charset FROM language WHERE panel = "a_panel" AND folder_name = :language';
	$select = $db->prepare($query);
	$select->bindParam(':language', $language, PDO::PARAM_STR);
	$select->execute();
	$row = $select->fetch(PDO::FETCH_ASSOC);
		
	return $row['charset'];
		
    }
	
}

?>
Link to comment
https://forums.phpfreaks.com/topic/300672-where-should-i-set-setcookie/
Share on other sites

Do you mean, like, where is the most appropriate place in code to set it? There isn't anything in there that's clearly "this is a point where the user has chosen a particular language", but the closest would be where you check $_GET.

if (isset($_GET['language']) && self::is_language_supported($db, $config, $_GET['language'])) {
	// setcookie(...)
	return $_GET['language'];
}
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.