Forum Moderators: coopster

Message Too Old, No Replies

I need Php redirect in wordpress

         

Tribe

10:28 pm on Jan 27, 2006 (gmt 0)

10+ Year Member



How can I redirect based on browser language using php in wordpress?

I tried this code; but it won't work & it screws up the wordpress & show's errors?


<? $user_lan = $HTTP_ACCEPT_LANGUAGE;

if($user_lan=='zh-sg') {
## Singapore
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-mo') {
## Macau SAR
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-hk') {
## Hong Kong SAR
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-cn') {
## China
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-tw') {
## Taiwan
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh') {
## Chinese
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='ja') {
## Japanese
$redir_url = "http://www.domain.com/japanese.html";

} elseif($user_lan=='ko') {
## Korean
$redir_url = "http://www.domain.com/korean.html";

}

header("Location: $redir_url");
exit;

?>

note that the pages/domains being redirected to are not the same domain that the code is placed on.
I placed the code in the head tag, it did not work, so I tried in the body tag & it did not work. all I get are wordpress display errors.

jatar_k

11:19 pm on Jan 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you are going to use the header function then you need to this before any output is sent to the browser. I would suggest doing this first, before anything else happens.

above the html, no white spaces before your <?php tag because even that causes your headers to be sent to the browser.

Tribe

12:02 am on Jan 28, 2006 (gmt 0)

10+ Year Member



if you are going to use the header function then you need to this before any output is sent to the browser. I would suggest doing this first, before anything else happens.
above the html, no white spaces before your <?php tag because even that causes your headers to be sent to the browser.

thank you, but this didnt work either? Is there something wrong with the code?

I put it at top of page before all the html, etc, & then when go to load page I get page can not be found, so it's definitly not working.

so I moved it to right above the header info, & get this on the page;

Warning: Cannot modify header information - headers already sent by (output started at /usr/www/virtual/user/www.domain.com/wp-content/themes/themename/header.php:3) in /usr/www/virtual/user/www.domain.com/wp-content/themes/themename/header.php on line 39

coopster

1:12 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The code looks fine, but as jk stated, you are outputting something before the header() function is being called. Review your code, look for any possible whitespace outside of the <?php ?> tags as well as any print or echo statements within the tags. Nothing, absolutely nothing, can be output to the browser prior to a header() function.

Tribe

2:10 am on Jan 28, 2006 (gmt 0)

10+ Year Member



I put it after the header information, at the bottom of header.php, & I get error;

Warning: Cannot modify header information - headers already sent by (output started at /usr/www/virtual/skull/www.domain.com/wp-content/themes/themename/header.php:4) in /usr/www/virtual/skull/www.domain.com/wp-content/themes/themename/header.php on line 105

I've tried pretty much ever possible spot & not one of them work.

coopster

2:18 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't think you understand what is being said. Here are some examples that will cause these errors:
<html><head> 
<?php header("Content-type: text/plain"); ?>
<title>
...

Wrong! Cannot output headers after you've already starting writing hmtl!

space character here, or even just a newline 
<?php header("Content-type: text/plain"); ?>
...

Wrong again! Cannot output headers after you've already starting writing hmtl! In this case, the blank space or even something as simple as a newline is considered output. You will get an error.

Somewhere in your code, prior to your header() function, you are outputting something. You will have to find it.

Tribe

2:32 am on Jan 28, 2006 (gmt 0)

10+ Year Member



THE PHP REDIRECT CODE WAS THE FIRST THING ON THE PAGE BEFORE ALL THE HTML! THAT DID NOT WORK!

Tribe

2:45 am on Jan 28, 2006 (gmt 0)

10+ Year Member



this is the header.php without the php redirect code;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">

<title><?php bloginfo('name');?> <?php if ( is_home() ) {?>&raquo; <?php bloginfo('description');?><?php }?> <?php wp_title();?></title>

<meta http-equiv="Content-Type" content="<?php bloginfo('html_type');?>; charset=<?php bloginfo('charset');?>" />
<meta name="generator" content="WordPress <?php bloginfo('version');?>" />

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url');?>" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo('rss2_url');?>" />
<link rel="alternate" type="text/xml" title="RSS .92" href="<?php bloginfo('rss_url');?>" />
<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="<?php bloginfo('atom_url');?>" />
<link rel="pingback" href="<?php bloginfo('pingback_url');?>" />

<?php wp_get_archives('type=monthly&format=link');?>
<?php wp_head();?>
<meta name="keywords" content="keywords deleted">
<META NAME="Author" CONTENT="author">
<META NAME="copyright" content="September 2005">
<META HTTP-EQUIV="imagetoolbar" CONTENT="no">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<META NAME="robots" content="index,follow">
<meta http-equiv="revisit-after" content="3 days">
<META NAME="Description" CONTENT="content">
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" L gen true for "http://www.domain.com" r (SS~~000 9 SS~~001 2 SS~~002 7 SS~~003 2 SS~~004 7 SS~~009 1))'>
<link rel="meta" href="http://domain.com/labels.rdf" type="application/rdf+xml" title="ICRA labels" />
<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

if (language.indexOf('zh') > -1 ¦¦ language.indexOf('zh-cn') > -1 ¦¦ language.indexOf('zh-hk') > -1 ¦¦ language.indexOf('zh-mo') > -1 ¦¦ language.indexOf('zh-tw') > -1 ¦¦ language.indexOf('zh-sg') > -1)
document.location.href = 'http://www.domain.be/China/chinese.html';
</script></head>
<body><script type='text/javascript' src='http://track.mybloglog.com/js/jsserv.php?mblID=#*$!xxxx'></script>

<div id="wrapper">

<div id="header">
<div id="title">
<h1><a href="<?php echo get_settings('home');?>"><?php bloginfo('name');?></a><br><div align="center"><a href="http://www.link.com"><img src="/flag_ja.gif" alt="link info" width="18" height="12" border="0"></a>&nbsp;<a href="http://www.link2.htm"><img src="/flag_ko.gif" alt="link 2" width="18" height="12" border="0"></a>&nbsp;<a href="http://www.link3.html?ref=xxxx"><img src="/flag_zh-CN.gif" alt="link3" width="18" height="12" border="0"></a></div></h1>
</div>
</div>

I placed the php redirect at the top;
like this;


<? $user_lan = $HTTP_ACCEPT_LANGUAGE;

if($user_lan=='zh-sg') {
## Singapore
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-mo') {
## Macau SAR
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-hk') {
## Hong Kong SAR
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-cn') {
## China
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh-tw') {
## Taiwan
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='zh') {
## Chinese
$redir_url = "http://www.domain.com/chinese.html";

} elseif($user_lan=='ja') {
## Japanese
$redir_url = "http://www.domain.com/japanese.html";

} elseif($user_lan=='ko') {
## Korean
$redir_url = "http://www.domain.com/Korea.htm";

}

header("Location: $redir_url");
exit;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">

<title><?php bloginfo('name');?> <?php if ( is_home() ) {?>&raquo; <?php bloginfo('description');?><?php }?> <?php wp_title();?></title>

<meta http-equiv="Content-Type" content="<?php bloginfo('html_type');?>; charset=<?php bloginfo('charset');?>" />
<meta name="generator" content="WordPress <?php bloginfo('version');?>" />

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url');?>" type="text/css" media="screen" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="<?php bloginfo('rss2_url');?>" />
<link rel="alternate" type="text/xml" title="RSS .92" href="<?php bloginfo('rss_url');?>" />
<link rel="alternate" type="application/atom+xml" title="Atom 0.3" href="<?php bloginfo('atom_url');?>" />
<link rel="pingback" href="<?php bloginfo('pingback_url');?>" />

<?php wp_get_archives('type=monthly&format=link');?>
<?php wp_head();?>
<meta name="keywords" content="keywords deleted">
<META NAME="Author" CONTENT="author">
<META NAME="copyright" content="September 2005">
<META HTTP-EQUIV="imagetoolbar" CONTENT="no">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<META NAME="robots" content="index,follow">
<meta http-equiv="revisit-after" content="3 days">
<META NAME="Description" CONTENT="content">
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.classify.org/safesurf/" L gen true for "http://www.domain.com" r (SS~~000 9 SS~~001 2 SS~~002 7 SS~~003 2 SS~~004 7 SS~~009 1))'>
<link rel="meta" href="http://domain.com/labels.rdf" type="application/rdf+xml" title="ICRA labels" />
<SCRIPT LANGUAGE="JavaScript">
if (navigator.appName == 'Netscape')
var language = navigator.language;
else
var language = navigator.browserLanguage;

if (language.indexOf('zh') > -1 ¦¦ language.indexOf('zh-cn') > -1 ¦¦ language.indexOf('zh-hk') > -1 ¦¦ language.indexOf('zh-mo') > -1 ¦¦ language.indexOf('zh-tw') > -1 ¦¦ language.indexOf('zh-sg') > -1)
document.location.href = 'http://www.domain.be/China/chinese.html';
</script></head>
<body><script type='text/javascript' src='http://track.mybloglog.com/js/jsserv.php?mblID=xxxxxxx'></script>

<div id="wrapper">

<div id="header">
<div id="title">
<h1><a href="<?php echo get_settings('home');?>"><?php bloginfo('name');?></a><br><div align="center"><a href="http://www.link.com"><img src="/flag_ja.gif" alt="link info" width="18" height="12" border="0"></a>&nbsp;<a href="http://www.link2.htm"><img src="/flag_ko.gif" alt="link 2" width="18" height="12" border="0"></a>&nbsp;<a href="http://www.link3.html?ref=xxxx"><img src="/flag_zh-CN.gif" alt="link3" width="18" height="12" border="0"></a></div></h1>
</div>
</div>

this did not work; so I then tried placing it after the closing div at the end, & this did not work...

eeek

2:50 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Put the redirect code on top. Don't have even a space or a newline before the first <? tag.

encyclo

2:57 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you're adding the code in the wrong place - the header.php within the template is not the first code parsed when generating a page in WordPress. The root-level index.php is called first, which is turn calls wp-blog-header.php which itself is setting several HTTP headers. You will need to integrate your code within wp-blog-header.php.

Tribe

4:11 am on Jan 28, 2006 (gmt 0)

10+ Year Member



YES, YES...

it was at the top, there are no spaces or lines before the <? & yes it was in header.php which IS the first file to be parsed, as index php is created by combination of header.php, sidebar.php, main.php, page.php, search.php, archives.php, etc, etc, etc...

index.php can not exist without header.php being first because header.php is the top of the page before the content, & header.php contains the head & the meta, therefore it is always first & always on top.

index.php is not an editable file.

jatar_k

8:17 am on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



regardless of where you think it is

everytime you get this
>> headers already sent by

then something is being output first, that is exactly what the error means, yelling at people and rudeness don't change this fact

Tribe

12:28 pm on Jan 28, 2006 (gmt 0)

10+ Year Member



regardless of where you think it is
everytime you get this
>> headers already sent by

then something is being output first, that is exactly what the error means, yelling at people and rudeness don't change this fact

First of all, I don't hear a single sound on this board, so I can not possibly be yelling, 2, you do not know me, therefore you can not call me rude.. What did I do to cause such response. I have not yelled or been rude to anyone. But now I will be rude in response to your incorrect accusation to me,, since your such a smart guy ,, explain to me why this code ALONE ON A SINGLE PAGE BY ITSELF gives the SAME ERROR & THERE IS NO HEADER TO OUTPUT. Matter of fact, the code alone on a page has nothing at all to output?

Even on non wordpress page with no other code, it is producing the same result.

jatar_k

3:41 pm on Jan 28, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if I paste that code exactly into a page and upload it, it gives me no errors, doesn't work but gives me no errors.

when I comment out the header line and echo $user_lan I get

en-us,en;q=0.5

so it won't work for me anyway but it does make me think that the value of $user_lan might be a little different than your script expects.

so are you getting another warning or error aside from the headers already sent? If so that the first warning would cause the headers error.

Moosetick

5:17 pm on Jan 30, 2006 (gmt 0)

10+ Year Member



Try this...

Make the first line of your page be ...

<?php ob_start();?>

and the last line be ...

<? ob_end_flush();?>

This will tell it not to worry about the header placement. I believe the flush clears out memory the process requires.