Php problem

I’ve been trying to get a redirect script to work but as I know nothing about php I am at a loss. I get a Warning:

[21-Apr-2015 13:08:52 UTC] PHP Warning: Cannot modify header information - headers already sent by (output started at /home/pintotou/public_html/Links/links.php:7) in /home/pintotou/public_html/Links/links.php on line 9

The script is this:

<!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="robots" content="noarchive, noindex, nofollow, noodp, noydir" />
 </head>
 <?php
 /* Redirect browser */
 header("Location: http://www.dpbolvw.net/click-7089264-10925976-1428564451000?url=http%3A%2F%2Fwww.accorhotels.com%2Flien_externe.svlt%3Fgoto%3Drech_geo_nobar%26nom_ville%3Ddelhi");
 /* Make sure that code below does not get executed when we redirect. */
 die();
 ?>
 <body>
 </body>
 </html>

The html

<a href="/Links/links.php" target="_blank" rel=”nofollow”>
4 hotels<img src="/Pinto/Logos/AccorSmallLogo.jpg" width="88" height="31" alt="Accor Hotels"></a>

PHP headers will only work as long as no content as been given to the browser to output. That example will only work if you do the following.

 <?php
 /* Redirect browser */
 header("Location: THE ADDRESS TO BE LINKED TO");
 /* Make sure that code below does not get executed when we redirect. */
 die();
 ?>
<!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="robots" content="noarchive, noindex, nofollow, noodp, noydir" />
 </head>
 <body>
 </body>
</html>

Hi Ryan (Jack of all trades: php too?)

I still get the same error

this is the html in India/Test.php

<a href="/Links/links.php" target="_blank" rel=”nofollow”>
4 hotels<img src="/Pinto/Logos/AccorSmallLogo.jpg" width="88" height="31" alt="Accor Hotels"></a>

this is the new /Links/links.php

<?php
 /* Redirect browser */
 header("Location: http://www.dpbolvw.net/click-7089264-10925976-1428564451000?url=http%3A%2F%2Fwww.accorhotels.com%2Flien_externe.svlt%3Fgoto%3Drech_geo_nobar%26nom_ville%3Ddelhi");
 /* Make sure that code below does not get executed when we redirect. */
 die();
 ?>
<!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>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <meta name="robots" content="noarchive, noindex, nofollow, noodp, noydir" />
 </head>
 <body>
 </body>
</html>

Make sure there’s nothing being outputted before that header. No spaces or anything. It should work. The error is the same, yes?

Remove all white space until yo ufind the culprit. Probably a random space or something is still there before the headers.

I wonder how lazy are people. It takes less than 10 seconds to copy few words from the error:

PHP Warning: Cannot modify header information

and google it. First page of results has tons of solutions.

And of course official manual has the answer:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Sorry, maybe that’s rude, but I’ve seen this question five times last week.

1 Like

Success!!!

Thank Ryan. This is one option. I’m still working on the other one which I posted in the Java section. Did you see it?

No, I will not take offense.

I did spend a few hours googling and understood it to be a problem with empty spaces. but not being as clever as you, I caould not work it out.

Thank you for the help you gave resolving the problem.

I don’t really know Java but I’ll take a look at it. Don’t expect any advice :slight_smile: .

Edit-Oh you mean Java script . That’s completely different from Java.

It’s very strange, because if you google for “PHP Warning: Cannot modify header information” first link in results gives the answer and spaces mentioned there too:

Unintentional:
Whitespace before <?php or after ?>

And I’m not trying to offense you, sorry.
I’m just want to say there is 98% chance someone has already encountered any problem you get.
Just google it.

1 Like

Ryan, I was referring to a good destination for your 6-week vacation!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.