My header script does not work. It says:
Warning: Cannot modify header information - headers already sent by (output started at C:\Mario\test\search.php:9) in C:\Mario\test\search.php on line 17
Here is my code:
if ($place == "Barrie")
{
header("Location: <!-- m --><a class="postlink" href="http://www.google.com">http://www.google.com</a><!-- m -->");
exit;
}
elseif ($place == "Toronto")
{
header("Location: <!-- m --><a class="postlink" href="http://www.weather.gov">http://www.weather.gov</a><!-- m -->");
exit;
}
Can anybody help me?
Thank You!is that all of your code?Yes, because I just started learning PHP and I just wanted to make a test page. But it doesn't work. make sure there's no space before the opening <?php tag and no space after the closing ?> tag.ok, I think you send a Location header like this:header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->");It doesn't need the <a href= <!-- m --><a class="postlink" href="http://www.htmlforums.com/archive/index.php/stuff">http://www.htmlforums.com/archive/index.php/stuff</a><!-- m -->.
Then, where is $place set? You CANNOT send headers after ANYTHING else has already been sent. For example...this works:<?php header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->"); ?>
<!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>
...
This does NOT work:
Warning: Cannot modify header information - headers already sent by (output started at C:\Mario\test\search.php:9) in C:\Mario\test\search.php on line 17
Here is my code:
if ($place == "Barrie")
{
header("Location: <!-- m --><a class="postlink" href="http://www.google.com">http://www.google.com</a><!-- m -->");
exit;
}
elseif ($place == "Toronto")
{
header("Location: <!-- m --><a class="postlink" href="http://www.weather.gov">http://www.weather.gov</a><!-- m -->");
exit;
}
Can anybody help me?
Thank You!is that all of your code?Yes, because I just started learning PHP and I just wanted to make a test page. But it doesn't work. make sure there's no space before the opening <?php tag and no space after the closing ?> tag.ok, I think you send a Location header like this:header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->");It doesn't need the <a href= <!-- m --><a class="postlink" href="http://www.htmlforums.com/archive/index.php/stuff">http://www.htmlforums.com/archive/index.php/stuff</a><!-- m -->.
Then, where is $place set? You CANNOT send headers after ANYTHING else has already been sent. For example...this works:<?php header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->"); ?>
<!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>
...
This does NOT work:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->"); ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...This also does not work:<?php echo ' '; ?>
<?php header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->"); ?>
<!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>
...
(This echos a space to the browser BEFORE setting a header)
This also does not work:<?php $whatever = 'whatever'; ?>
<?php header("Location: <!-- m --><a class="postlink" href="http://www.example.com/">http://www.example.com/</a><!-- m -->"); ?>
<!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>
...
(If you highlight the first line of this, you will see that there is a space after the ?> tag. That space gets sent to the browser before the header)
You can read all about it here (<!-- m --><a class="postlink" href="http://us4.php.net/manual/en/function.header.php">http://us4.php.net/manual/en/function.header.php</a><!-- m -->).The forums add the a href stuff.Originally posted by Josh
is that all of your code?
I am thinking not
in C:\Mario\test\search.php on line 17
line 17 is the header so he has a bunch other stuff above that.
turn the url off then you won't get the links like that.If you enable Out put buffering at the very top of you page, you can modify headers and cookies any time you want
Use the following at the top of your page
ob_start();
And this at the bottom of your page or the point where you want all output to be sent to the browser
ob_end_clean();
This works like a charm for moi!