Header ("Location") in php does not work [closed]

0

I have a php code at the beginning of a document, but the header function does not work. My code is as follows:

<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

function lang() {
    $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
    switch ($lang) {
        case "es":
            return 'es';
            break;
        case "ca":
            return 'cat';
            break;
        case "en":
            return 'en';
            break;
        default:
            return 'en';
            break;
    }
}

if (!isset($_GET['lang'])) {
    header("Location: index.php?lang=" + lang()); ///// Aquí
}
else {
    if ($_GET['lang'] != "es" && $_GET['lang'] != "cat" && $_GET['lang'] != "en")
        header("Location: index.php?lang=" + lang()); ///// Aquí
    else echo $_GET['lang'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Gabinete</title>
<?php include_once("pred/meta.php"); ?>
</head>
<body>
<?php include_once("pred/header.php"); ?>
</body>
</html>
    
asked by Gerry Studios 18.04.2017 в 18:33
source

2 answers

1

I have not had the opportunity to test your code, but from what I see, you are concatenating with + and in PHP it is concatenated with .

    
answered by 18.04.2017 / 20:06
source
1

Try this:

$sLang = lang();
header("Location: index.php?lang=$sLang");

Or if you prefer, use this (recommended)

$sLang = lang();
echo "<script>location.href = 'index.php?lang=$sLang';</script>";
    
answered by 18.04.2017 в 18:56