Andy_Kemp Posted January 27, 2016 Share Posted January 27, 2016 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']; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/300672-where-should-i-set-setcookie/ Share on other sites More sharing options...
ginerjm Posted January 27, 2016 Share Posted January 27, 2016 To help you figure this out I would point you to the php manual. It says exactly where you should do it. In fact, where you HAVE to do it - "before any output is sent". Quote Link to comment https://forums.phpfreaks.com/topic/300672-where-should-i-set-setcookie/#findComment-1530471 Share on other sites More sharing options...
requinix Posted January 27, 2016 Share Posted January 27, 2016 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']; } Quote Link to comment https://forums.phpfreaks.com/topic/300672-where-should-i-set-setcookie/#findComment-1530476 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.