function header

Discussion in 'PHP' started by ycc, Aug 7, 2010.

  1. #1
    Hello,

    I have been trying out the function "header" (to try to avoid caching).

    The problem is that I do not understand its mechanism of action.

    Below is a very simple web-page. If the header is included, the webpage will be interpreted as text and "line one" and "line two" will appear on two different lines in the browser. If no header is called the page will be interpreted as HTML and everything will appear on one line in the browser.

    However, when I look at the source code in the browser, I can only see the text of the page. Nothing is added if I call the function header.

    What is the mechanism of action of the function header. In the simple example below, how does the function header tell the browser to skip HTML-rendering?

    Thanks.

    <? header('Content-type: text/plain'); ?>
    line one
    line two
    Code (markup):
     
    ycc, Aug 7, 2010 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    If you don't specify a header, PHP sends a default text/html header.

    In HTML, a line break, or any amount of spaces or even tabs are treated as a single space. If you want a line break in HTML, you have to use the <br /> tag. If you want more than one space, you have to use &nbsp;.
     
    nico_swd, Aug 7, 2010 IP
  3. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks nico_swd, (and also thanks for help you gave me in the past)

    But I still do not understand why I cannot see the header? Shouldn't I be able to see the header when I make ctrl/u in FF (view source).

    Maybe I don't understand what is included in a header? Nothing changes in the source of the web-page whether I include or do not include this call to function header. How can it then affect the web-page contents?
     
    Last edited: Aug 7, 2010
    ycc, Aug 7, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    You cant view php only html source codes, php happens on the server that hosts the files, not in the browser.
     
    MyVodaFone, Aug 7, 2010 IP
  5. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #5
    You can "see" the header by using a 3rd party tool such as live http headers.
     
    Kaizoku, Aug 7, 2010 IP
    ycc likes this.
  6. ycc

    ycc Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Arigatou Kaizoku.
    With live http headers I can now see many things I wasn't aware of before.
    I have seen tags like
    <meta http-equiv="Content-Type" content="text/[B]plain[/B]; charset=utf-8"> 
    
    Code (markup):
    But I don't think they work for web-pages, maybe for e-mail?

    It seems the header is "part of the source code" but cannot be read in the browser without this plug-in.
    http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

    Thanks for all the replies.
     
    ycc, Aug 7, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Wrong can be also done either via CURL or get_headers()
     
    danx10, Aug 8, 2010 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    Both of those will cause an infinite loop if called to the same location as the script.
     
    Kaizoku, Aug 8, 2010 IP