Jump to content
Larry Ullman's Book Forums

Manipulating Http Headers Chapter 8 Tips


Recommended Posts

Hi Larry,

 

You include two tips at the end of Chapter 8 that I have questions about.

 

The first one is:

 

The headers_sent() function returns TRUE if the page has already received HTTP headers and the header()function can't be used.

 

Could you please explain an example of this happening.

 

 

The second tip is:

 

Using the GET method trick, you can pass values from one page to another using header():

$var = urlencode('Pass this text');

header ("Location: page.php? message=$var");

 

When would you want to do this? Please give an example.

 

THANK YOU for your help!

JJ

Link to comment
Share on other sites

The headers_sent() function returns TRUE if the page has already received HTTP headers and the header()function can't be used.

 

Could you please explain an example of this happening.

 

Could you clarify what you mean by "this": using the code itself or a situation in which headers have already been sent?

 

 

Using the GET method trick, you can pass values from one page to another using header():

$var = urlencode('Pass this text');

header ("Location: page.php? message=$var");

 

When would you want to do this? Please give an example.

 

 

Really, anytime you need to send more than a one-word string along in a URL. For example, one page might do some validation or whatever and pass a message to another page.

 

 

Link to comment
Share on other sites

Thanks for your quick reply and answer.

 

In answer to your request that I clarify "this"...

 

I could use examples of both: using the code itself and a situation in which headers have already been sent.

 

I'm just not wrapping my head around this one.

 

Thanks for your help.

Link to comment
Share on other sites

You're quite welcome. As for an example, the code would just be something along the lines of

if (!headers_sent()) {
header(...);
} else {
print 'Would have sent a header but I could not.';
}

 

In a live site with things working as they should, you should have already worked out all the headers already sent issues. Sometimes, when developing something, you may appreciate using code like this to avoid getting secondary, confusing errors. For example, a script does X and then redirects the browser. If the script has an error before the header call, then you'll see the error and the headers already sent error, which can be confusing to some. So you could use that code to prevent that from happening.

 

Using code like the above isn't obligatory, in the sense that I don't use it commonly, but it has it's merits.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...