[Résolu] SPIP 4.4.20 and PHP header() calls

Hello all,
I’ve noticed that since the latest version, inline PHP header() calls inside squelettes/template.html files do not work anymore as before.

I’ve moved the code to pipelines executed before the template even loads (the right way alas).

BUT: I still wanted to point out that the new compiler extracts header() calls no matter where they are, even inside if.

if(false){
header("Location: /");
}

Will be extracted, even if it would never evaluate.
It’s evaluated the same as

if(true){
header("Location: /");
}

or

header("Location: /");

I agree that it’s better to not put redirect headers in SPIP templates, but it would be nice to provide a warning or a mitigation.

Would any of these be a good solution?

  1. log an error at the template level whenever a header() is found in squelettes, and notify that this behavior is not only deprecated but unstable: it does not behave logically. Inform that the solution is to either use #HEADER at the top of the file, or use redirige_par_entete() when it’s a redirect.
  2. If there are other things that should never be in template files (exit calls maybe?), also log or throw an error at the template

What do you think?

We do not even aware of the existence of these cases!
Headers should be added with #HTTP_HEADER if needed.

In fact, you’re certainly right the new version catch headers directly in SPIP templates files with some PHP.

One more issue so…

J’ai ce cas dans un site qui était précédemment en français et espagnol, et où ça a été éclaté en 2 sites, l’un en français, l’autre en espagnol.

Et dans sommaire.html du site Espagnol, j’ai ceci :

<?php
if ('#ENV{lang}'=='fr') { 
header('Status: 301 Moved Permanently', false, 301); 
header('Location: https://www.sitefrancais.tld/');
}
?>

Et effectivement, comme @ursrig, j’ai systématiquement la redirection.

Ça s’écrirait de manière beaucoup plus propre en

[(#ENV{lang}|=={es}|sinon_interdire_acces{https://www.sitefrancais.tld/,301})]

Oh, brillant !

Merci @cerdic

1 « J'aime »

La version 4.4.21 est sortie. Et il est important de mettre à jour :wink:

1 « J'aime »

Merci, cela a été résolu très rapidement.

1 « J'aime »