PHP文件中的页面顶部是什么意思 [英] what means by top of the page in php file

查看:106
本文介绍了PHP文件中的页面顶部是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 我在某处读到 ob_start()应该放在页面顶部。而
  • 某处我读到 session_start()应该放在页面的顶部。

  • 某处我读了 header()应放置在页面顶部。

  • 在某处我读了 include() require()应该放在页面的顶部。 >


我很困惑应该在顶部写什么以及按何种顺序放置?在之前是


  • < / c> < html> < head>之前的HTML>

  • ; < head>




  • 请告诉我什么是所有这些函数的实际顺序。

    像我们必须放置ob_end_flush()一样的方式。和其他函数,位于< html> 之后或< / body> 之后的页面底部是页面底部函数的顺序。为了理解你写的语句的价值,为了理解你写的语句的价值,你需要对你提到的功能的操作有一些基本的了解。



    让我们从 session_start() header()调用:

    第一个函数的名字意味着什么,它开始一个会话。


    由于HTTP协议的无状态性质,需要一些机制来记住页面请求之间的状态。这可以通过会话来实现。尽管会话在PHP早期有时会通过在链接( someurl?sessionId = someSessionHash )中传递会话ID进行传播,但现在这被认为是不好的做法。



    现在,会话主要通过使用cookie来记录(在早期他们广泛使用的地方,不要误解我的意思)。这个会话cookie(与普遍的看法相反,它只不过是一个普通的cookie,只有会话ID,通常只会在关闭浏览器后过期)随每个后续页面请求一起发送到浏览器。这里是catch的地方:一个cookie作为响应的头部(意思是在实体之前)发送,如下所示:

      //为了简洁,我省去了很多其他的头文件
    HTTP / 1.x 200 OK
    Date:Sun,31 Jan 2010 09:37:35 GMT
    Cookie :SESSION = DRwHHwAAACpes38Ql6LlhGr2t70df //这里是你的Cookie头

    //响应头后,来的实际内容:
    //响应体,例如:
    < HTML> ;
    < head>
    < / head>
    < body>
    < / body>
    < / html>

    现在,因为响应头必须在响应正文之前发送,所以您需要调用 session_start() header(),然后输出正文内容。原因如下:如果您在调用 session_start()标题前输出任何响应主体内容(可能与空格字符一样简单) ),PHP会自动输出响应标题。这是因为HTTP响应必须在响应主体之前首先发送响应头。正是这种情况经常导致臭名昭着的 Warning:头文件已经在PHP中发送了警告。换一种说法;一旦PHP发送了标题,因为它也必须发送主体数据,它不能再添加任何标题。



    所以,现在你已经理解了HTTP协议,您可以采取一些措施来防止这种情况发生。这是我们来到下一个函数的地方:

    ob_start ob_flush 等...:

    在默认设置中,PHP通常会立即输出任何东西。因此,如果您输出任何响应正文内容,则会自动首先发送标头。

    但PHP提供缓冲输出机制。这是 ob _ * 系列函数。用 ob_start ,你告诉PHP开始缓冲。并且用 ob_flush 告诉PHP刷新缓冲区;换句话说,将缓冲区的当前内容输出到标准输出。


    使用这些缓冲机制,您仍然可以在输出正文数据后为响应添加标题,因为您还没有真正发送正文数据,所以您只需缓冲它将在稍后通过调用 ob_flush ob_end_flush 以及您有什么。



    请记住,使用 ob _ * 函数通常是一种代码异味。换句话说(这就是为什么在顶部做某些事情很重要),然后用它来弥补糟糕的设计。有人忘记正确设置他们的操作顺序,并采用输出缓冲来绕过会话戏剧。 / p>

    说完这一切,你可以很容易地明白为什么输出html和/或其他主体内容应该持续到最后。除此之外,我强烈建议您将PHP代码与输出代码分开。因为它更容易阅读和理解。开始这样做的一个好方法是将实际的html放在主要的<?php?> 代码块之后。但还有其他方法,这超出了这个问题范围。

    然后最后关于 include 需要调用。将这些放在你的php文件的顶部通常需要澄清。它将这些呼叫很好地保存在一个地方。但请记住,如果在调用 session_start() header()之前没有输出任何文件使用输出缓冲,你再次搞砸了。


    • i read somewhere that ob_start() should be places top of the page. whereas
    • somewhere i read that session_start() should be placed on the top of the page.
    • somewhere i read header() should be placed on the top of the page.
    • somewhere i read include() or require() should be placed on the top of the page.

    i m getting confused what should be written on the top and in which order ther are placed ? and what means by on the top??? is it

    • before <html> or
    • after <html> or before <head> or
    • after <head>

    please tell me what is real order of all these function

    like same manner where we have to put ob_end_flush(); and other function, at the bottom of the page after <html> or after </body> and what is the order of functions that comes on the bottom of the page

    解决方案

    In order to understand the value of the statements you have written you need to have some basic understanding of the operations of the functions you mention. I'll try to break them down here.

    Let's start with session_start() and header() calls:
    The first function does exactly what the name implies; it starts a session.

    Due to the stateless nature of the HTTP protocol, there is a need for some mechanism that can remember state between page requests. This can be achieved with sessions. Although sessions, in the early days of PHP where sometimes propagated by passing along the session ID in links ( someurl?sessionId=someSessionHash ), this, nowadays, is considered bad practice.

    Nowadays, sessions are predominantly kept track of by using a cookie (in the early days they where widely used too, don't get me wrong). This session cookie (which, contrary to popular belief, is nothing more than a normal cookie, with merely the session ID in it, that (usualy) simply expires after you close your browser) is sent along to the browser with each subsequent page request. And here is where the catch is: A cookie is sent as a header of the response (meaning before the actual body), like so:

    // I've left out a lot of other headers for brevity
    HTTP/1.x 200 OK
    Date: Sun, 31 Jan 2010 09:37:35 GMT
    Cookie: SESSION=DRwHHwAAACpes38Ql6LlhGr2t70df // here is your Cookie header
    
    // after all response headers come the actual content: 
    // the response body, for instance:
    <html>
        <head>
        </head>
        <body>
        </body>
    </html>
    

    Now, because response headers must be sent before the response body, you need to put a call to session_start() and header() before any body content is output. Here's why: if you output any response body content (could be something as simple as a whitespace character) before a call to session_start() or header(), PHP will automatically output the response headers. This is because a HTTP response must have the response headers sent out first before the response body. And it is exactly this that often leads to the infamous Warning: headers already sent warning in PHP. In other words; once PHP has sent out the headers, because it had to send body data too, it cannot add any headers anymore.

    So, now that you understand this about the HTTP protocol, there are some measurements you can take to prevent this from happening. And this is where we come to the next function(s):

    ob_start, ob_flush, etc...:
    In a default setup PHP usualy outputs anything immediately. Therefor, if you output any response body content, headers are automatically sent first.
    But PHP offers mechanisms of buffering output. This is the ob_* family of functions. With ob_start you tell PHP to start buffering. And with ob_flush you tell PHP to flush the buffer; in other words output the current content of the buffer to the standard output.

    With these buffering mechanisms you can still add headers to the response, after you have output body data, because you haven't actually sent body data yet, you have simply buffered it, to be output later with a call to ob_flush or ob_end_flush and what have you.

    Keep in mind though, that using ob_* functions is more than often a code smell. In other words (and this is why it is important to do certain stuff at the top), it is then used to make up for poor design. Somebody forgot to set up their order of operations properly and resorts to output buffering to circumvent this header and session drama.

    Having said all this, you can easily see why the outputting of html and/or other body content should come last. Apart from that, I strongly recommend you to separate PHP code from output code anyway. Because it is much more easy to read and understand. And a good way to start doing that is having the actual html come after the main <?php ?> code block. But there are other ways as well, which is beyond this questions scope.

    Then lastly about the include and require calls. To have these at the top of your php files is usually ment to be clarifying. It keeps these calls nicely in one place. But keep in mind, that if one of these files output anything before you call session_start() or header() without using output buffering, you're screwed again.

    这篇关于PHP文件中的页面顶部是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆