Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
php header location to redirect to a section
Hi folks,
In a bi-lingual TXP website, I need to redirect my visitors to a corresponding section (fr or en). This first part is grabbed from browser language preferences. Now I’m trying to redirect to the correct section.
It seems that php header location don’t work within TXP. Here is my code :
$langs = explode(",",$_SERVER["HTTP_ACCEPT_LANGUAGE"]);
$_SESSION['language'] = $langs[0];
if($langs[0] 'fr' || $langs[0] 'fr-fr') {
$_SESSION['language'] = 'fr';
//header('location: /fr');
… and so on.
The last line failed.
Could you tell me if is it possible? and how to do that?
Many thanks by advance.
Regards,
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
#2 2008-10-28 12:16:02
- tbo
- Member
- Registered: 2008-10-24
- Posts: 12
Re: php header location to redirect to a section
Hi,
first of all you have to specify your header-location with an absolute url, like “http://yoursite/fr” – see: php.net
What error did you get when executing your code? If it’s:
Cannot modify header information - headers already sent [...]
you should try to put the script-snippet, you use for redirecting, on the top of your template, before something is send within the header – then it should work.
Best regards,
Tobi
Offline
Re: php header location to redirect to a section
Nope. I hadn’t any errors at all.
The script below is send before any tags and doctype stuff. And it failed.
But I’d found a solution :)
Within my default template, I’d put this :
<txp:if_section name=""><txp:php>if($_SESSION['language'] == 'fr') { header ('Location: '.hu.'fr'); } else { header ('Location: '.hu.'en'); }</txp:php><txp:else />(... and so on ...)</txp:if_section>
So now, I can redirect to my corresponding section for the default browser language. Cool.
BTW, thanks lot for your answer Tobi.
Have a nice day.
Best regards,
Last edited by Pat64 (2008-10-28 15:53:38)
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
#4 2008-10-28 18:02:08
- tbo
- Member
- Registered: 2008-10-24
- Posts: 12
Re: php header location to redirect to a section
That’s odd – I tried a similar script within <txp:php> and it worked perfectly for me … but the main thing is that it works now.
Offline
Re: php header location to redirect to a section
So, could you share your code?
Patrick.
Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.
Offline
#6 2008-10-29 11:17:32
- tbo
- Member
- Registered: 2008-10-24
- Posts: 12
Re: php header location to redirect to a section
Hi,
well there is nothing special about it, I just put this one in my default-template:
<txp:php>
session_start();
if($_SESSION['lang'] == 'de')
header('location: http://example.com');
</txp:php>
And this one in a second template:
<txp:php>
session_start();
$_SESSION['lang'] = 'de';
echo '<a href="'.hu.'index.php?'.htmlspecialchars(SID).'">Main</a>';
</txp:php>
Everytime I access the default-page via “Main”-link, the new header-location is send and I am redirected. My php-skills are not the best, so maybe I made a mistake somewhere, but I think it should work the same way in your script.
Best regards,
Tobi
Offline