Files
freeshop/lang.php
T
2026-08-14 10:55:06 +08:00

29 lines
890 B
PHP

<?php
/**
* 语言切换页(服务端)。
* 用法:<a href="lang.php?lang=en_US">English</a>
* 本页设置 $_SESSION['lang'] + cookie 后,重定向回来源页(或首页)。
*/
require __DIR__ . '/includes/init.php';
$lang = trim($_GET['lang'] ?? '');
$allowed = [];
foreach (Lang::available() as $l) {
$allowed[] = $l['code'];
}
if ($lang !== '' && in_array($lang, $allowed, true)) {
$_SESSION['lang'] = $lang;
setcookie('fnw_lang', $lang, time() + 31536000, '/');
Lang::setLocale($lang);
}
// 重定向回来源页,避免跳转到站外
$ref = $_SERVER['HTTP_REFERER'] ?? '';
$host = parse_url((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://') . ($_SERVER['HTTP_HOST'] ?? ''), PHP_URL_HOST);
if ($ref === '' || parse_url($ref, PHP_URL_HOST) !== $host) {
$ref = '/';
}
header('Location: ' . $ref, true, 302);
exit;